我创建了一个计算学费的程序。我几乎完成它,但有一些错误。我不知道为什么这是错的。
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说“symbol:variable hour”。
答案 0 :(得分:2)
在方法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
类,以便它具有这些私有数据成员,并将静态方法移入其中。然后,main函数可以创建一个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
}
...
}
这样,您的代码就变得更容易理解,并且您可以避免重复。