我创建了一个程序来计算学费。我差不多完成了,但是有一些错误。我不知道为什么这是错的。
import java.util.*;
public class Tuition {
public static void main (String[] args) {
Scanner kb = new Scanner(System.in);
int hour = getHours();
char major = getMajor();
char status = getStatus();
displayTuition();
}
public static int getHours() {
Scanner kb = new Scanner(System.in);
System.out.print("How many credit hours did you take:");
int hour = kb.nextInt();
if (hour >= 12) {
int tuitionfee = 800;
} else {
tutionfee = 70*hour;
}
return hour;
}
public static char getMajor() {
Scanner kb = new Scanner(System.in);
System.out.print("What's your major?");
char major = kb.nextLine().charAt(0);
if (major == 'C'||major == 'c') {
System.out.println("Computer Science");
int labfee = 25;
} else if (major == 'O'||major == 'o') {
System.out.println("Other Science");
int labfee = 35;
} else if (major == 'X'||major == 'x') {
System.out.println("Non-Science");
int labfee = 0;
}
return major;
}
public static char getStatus() {
Scanner kb = new Scanner(System.in);
System.out.print("You are in state student or not(yes for Y/y,no for N/n)");
char state = kb.nextLine().charAt(0);
if (state == 'Y'||state == 'y') {
System.out.println("In State");
int statusfee = 5;
} else if (state == 'N'||state == 'n') {
System.out.println("Out-of-State");
int statusfee = 5+20%(hour+labfee);
} else {
System.out.print("FALSE");
}
return state;
}
public static void displayTuition() {
System.out.println("Credit Hours:" +int hour);
System.out.println("Major:" +char major);
System.out.println("Residencey" +char state);
}
}
错误的部分是
public static void displayTuition() {
System.out.println("Credit Hours:" +int hour);
System.out.println("Major:" +char major);
System.out.println("Residencey" +char state);
}
这样,Java会说“ Tuition.java:56:错误:'。class'预期”和“ Tuition.java:56:错误:';” 预期的”。然后我尝试了:
public static void displayTuition() {
System.out.println("Credit Hours:" + hour);
System.out.println("Major:" + major);
System.out.println("Residencey" + state);
}
但这仍然是错误的,我记得我做了类似的事情,这是对的。这样,java会说“符号:可变小时”。
在方法内部,displayTuition
您正在使用不在范围内的三个变量。当它们在中被声明main
(调用displayTuition()
)时,该方法不会“知道”它们。为了使它们在范围内,需要将它们作为参数传递给displayTuition()
方法(最简单的解决方案)。像这样:
public static void main (String[] args) {
int hour = getHours();
char major = getMajor();
char status = getStatus();
displayTuition(hour, major, status);
}
// other methods
...
public static displayTuition(int hour, char major, char status) {
System.out.println("Credit Hours:" + hour);
//etc
}
较难(但更好的是IMO)选项是,您可以重组Tuition
类,使其具有这些私有数据成员,然后将静态方法移入该类。然后,主函数可以创建一个Tuition对象并在其上调用相关方法。
像这样的东西:
public class Tuition {
private int hour;
private char major;
private char status;
public Tuition(Scanner kb) {
// Initialize variables using methods that were initially static
this.hour = this.getHours(kb); //Pass in the scanner, since you use it all over the place.
...
}
public static void main (String[] args) {
Scanner kb = new Scanner(System.in);
Tuition tuition = new Tuition();
tuition.displayTuition();
}
public int getHours(Scanner kb) {
// Do the processing you have written
}
// other methods for major and status
public void displayTuition() {
System.out.println("Credit Hours:" + this.hour);
// etc
}
...
}
这样,您的代码将变得更易于理解,并且可以避免重复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句