-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