我是 java 新手,我已經被難住了。我正在嘗試制作一種允許您在控制臺中創建飲料的方法。使用 if 陳述句并設定正確的大小和飲料型別時,我遇到了困難。當我運行代碼時,一切正常,直到制作它,所以當你輸入某個數字時,它會設定飲料的大小。如果我為分配給 Short 的 size 選項輸入 2,它會將 Demi 設定為尺寸,即使我輸入 2 時它應該設定 Short,當我輸入 1 時它應該設定 Demi。順便說一句,方法是在一個子類中擴展一個超類,并且該方法將從主類中呼叫。如果您需要更多資訊,或者如果代碼不足以說明我具體在做什么,請告訴我,我會盡力提供更多資訊。
public void makeDrink(Scanner input) {
showOptions();
int choice = input.nextInt();
input.nextLine();
while (choice != 0) {
if (choice > 4 || choice < 0) {
System.out.println("You made a invalid choice, please reset");
}
/*
* Chose Size of the drink
*/
if (choice == 1) {
if(choice < 0 || choice > 6) {
System.out.println("You made a invalid choice, please try again");
}
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Choose the size");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("|1. Demi (3 oz)|2. Short (8 oz)|3. Tall (12 oz)|4. Grande (16 oz)|5. Venti (20 oz)|6. Trenta (30 oz)|");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
input.nextInt();
// first issue starts here
if (choice == 1) {
setSize("Demi");
}
if (choice == 2) {
setSize("Short");
}
// first issue ends here
}
/*
* Choose Drink
*/
if (choice == 2) {
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Choose the Drink");
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("|1. Coffee|2. Tea|");
System.out.println("~~~~~~~~~~~~~~~~~~");
String type = input.nextLine();
// second issue starts here
if (choice == 1) {
setType("Coffee");
}
if (choice == 2) {
setType("Tea");
}
// second issue ends here
showOptions();
choice = input.nextInt();
}
}
uj5u.com熱心網友回復:
您需要洗掉此陳述句
if (choice == 1) { //this statement
if (choice < 0 || choice > 6) {
System.out.println("You made a invalid choice, please try again");
}
否則,choice將設定為 1。條件if (choice < 0 || choice > 6) 始終為真。
此外,條件:
if (choice == 1) {總是trueif (choice == 2)總是false。
您需要洗掉掃描下一行,這對我來說似乎毫無意義。
int choice = input.nextInt();
input.nextLine(); //remove this
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510384.html
上一篇:如何為if陳述句設定IntellIJ的scala格式?
下一篇:Python串列理解忽略無結果?
