我的回圈有問題。我試圖讓用戶輸入一個數字選擇,如果他們選擇一個專案會重復,如果他們輸入 5 則退出。但是,在運行它時,如果我選擇 2 個專案,則需要我輸入 5 兩次才能退出回圈.
我很感激任何幫助。
public static void coffee() {
double espressoP = 4.50;
double latteP = 4.50;
double cappuccinoP = 6.00;
double coldBrewP = 4.00;
int coffeeChoice = 0;
boolean exit = true;
System.out.println("=== Select Coffee: ===");
System.out.println("1. Espresso $4.50");
System.out.println("2. Latte $4.50");
System.out.println("3. Cappuccino $6.00");
System.out.println("4. Cold Brew $4.00");
System.out.println("5. Quit coffee selection.");
System.out.println("Select a coffee [Enter 1 - 5]:");
while (exit = true) {
coffeeChoice = scnr.nextInt();
if (coffeeChoice == 1) {
System.out.println("Added 1 Espresso.");
C1 = 1;
totPrice = totPrice espressoP;
System.out.println(C1 " " totPrice);
coffee();
}
else if (coffeeChoice == 2) {
System.out.println("Added 1 Latte.");
C2 = 1;
totPrice = totPrice latteP;
System.out.println(C2 " " totPrice);
coffee();
}
else if (coffeeChoice == 3) {
System.out.println("Added 1 Cappuccino.");
C3 = 1;
totPrice = totPrice cappuccinoP;
System.out.println(C3 " " totPrice);
coffee();
}
else if (coffeeChoice == 4) {
System.out.println("Added 1 Cold Brew.");
C4 = 1;
totPrice = totPrice coldBrewP;
System.out.println(C4 " " totPrice);
coffee();
}
else if (coffeeChoice == 5) {
exit = false;
break;
}
else {
System.out.println("Invalid entry. Enter a selection between 1 and 5.");
scnr.next();
}
}
uj5u.com熱心網友回復:
更新。同意評論。exit = true是一個錯字, break 將使它無效。多次要求輸入“5”的原因是因為遞回。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477429.html
標籤:爪哇
下一篇:如何使程式以升序而不是降序計數?
