-2
我正在为我的课程工作。该程序运行良好,直到它击中我的最终程序,我一直在想它已经完成了2个小时左右。为什么在循环工作正常时不这样做?
这是它搞乱的代码。
do {
System.out.println("Do you want to end program? (Enter n or y):");
endProgram = Input.next();
if(!endProgram.equals("y") || (!endProgram.equals("n"))){
System.out.println("Do you want to end program? (Enter n or y):");
}
if (endProgram.equalsIgnoreCase("n")){
endProgram = "n";
aui = true;
}
if (endProgram.equalsIgnoreCase("y")){
endProgram = "y";
aui = true;
}
} while(aui = false);
我试图搞乱其他如果然后切换到if。完整的代码是
public static String endProgram = null;
public static void main(String[] args) {
String MUR = "--------------Monthly Use Report--------------";
int minutesAllowed;
int minutesUsed = 0;
int minutesOver;
double totalOwed;
double monthlyRate = 74.99;
double minOver = 0.20;
double realOwed;
boolean valid = false;
boolean over = false;
boolean aui = false;
Scanner Input = new Scanner(System.in);
System.out.println("Welcome to the Cell Phone Minutes Calculator.");
do {
do {
System.out.println("Please input the amount of minutes you were allowed to use per month.");
System.out.println("Please Enter a value between (200 - 800)");
minutesAllowed = Input.nextInt();
} while (minutesAllowed <= 199 || minutesAllowed >= 801);{
}
do{
try{
System.out.println("How many minutes were used during the previous month?");
minutesUsed = Input.nextInt();
if(minutesUsed <= 1){
System.out.println("--Invalid Input! Please use a positive number.--");
} else {
valid = true;
}
} catch(Exception e){
System.out.println("Invalid Input! Please try again.");
Input.next();
}
}while(!valid);
minutesOver = minutesAllowed - minutesUsed;
if(minutesAllowed >= minutesUsed){
System.out.println("You were not over your minutes for the month!");
} else {
System.out.println("You were over your minutes by "+ Math.abs(minutesOver));
over = true;
}
totalOwed = (Math.abs(minutesOver))*(minOver);
realOwed = totalOwed+monthlyRate;
System.out.println(MUR);
System.out.println("Minutes allowed were "+ minutesAllowed);
System.out.println("Minutes used were "+ minutesUsed);
if(over){
System.out.println("Minutes over were "+ Math.abs(minutesOver));
System.out.println("Total due is $"+ realOwed);
} else {
System.out.println("Total due is $"+ monthlyRate);
}
do {
System.out.println("Do you want to end program? (Enter n or y):");
endProgram = Input.next();
if(!endProgram.equals("y") || (!endProgram.equals("n"))){
System.out.println("Do you want to end program? (Enter n or y):");
}
if (endProgram.equalsIgnoreCase("n")){
endProgram = "n";
aui = true;
}
if (endProgram.equalsIgnoreCase("y")){
endProgram = "y";
aui = true;
}
} while(aui = false);
}while((endProgram.equalsIgnoreCase("n")) && (aui = false));
}
}
很抱歉,如果代码是马虎不得。当我运行程序时,它会正常运行,除非我把两个不正确的用户输入。例如,
程序运行//
--------------每月使用报告--------------
允许分钟,使用分别为500
分钟450
分钟以上进行50
应付总额为$ 84.99
你想结束程序吗? (输入n或y):
g
是否要结束程序? (输入n或y):
如果我添加Input.Next();嵌套如果陈述到
if(!endProgram.equals("y") || (!endProgram.equals("n"))){
System.out.println("Do you want to end program? (Enter n or y):");
endProgram = Input.next();
它正确显示它。我试图搞乱整个项目中的大量循环。如果有人能帮助我,我将非常感激。对不起,如果这是困惑,我会回应,如果你们有任何问题。预先感谢任何回应,并对由此造成的不便深表歉意。
总是喜欢使用布尔值,而不是直接比较它们彼此。 'while(!aui)'比'while(aui == false)'更容易阅读,更难以犯错误。 – azurefrog
@azurefrog是的,这是最好的做法。 – rajuGT
感谢一群我坐在这里,我知道这是简单的两个小时浪费...谢谢吨家伙! – froxtman1337