我需要幫助讓這個循環工作。這可能是因爲我真的不知道如何使用循環或if語句。但我覺得我需要經過10Java循環:爲什麼此循環沒有按預期停止?
import java.util.Scanner;
public class Foothill {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int numPurchases, totalPurchased;
boolean correct;
String menu;
numPurchases = 0;
totalPurchased = 0;
correct = false;
while(!correct)
{
System.out.println("Menu:");
System.out.println(" P (Process Purchase)");
System.out.println(" S (Shut down)");
menu = scanner.nextLine();
if (menu.charAt(0)=='S' || menu.charAt(0)=='s'){
System.exit(0);
} else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){
continue;
}
System.out.println("Your choice: " + menu.charAt(0));
System.out.println("How many yougurts would you like to buy?");
numPurchases = scanner.nextInt();
totalPurchased += numPurchases;
if (totalPurchased > 0 || totalPurchased < 10){
System.out.println("you have purchased " + totalPurchased + " yogurts");
} else if (totalPurchased >= 10){
System.out.println("Congradulations you have recieve a free Yogurt");
System.out.println("Remainding yogurt credits: " + (totalPurchased-10));
}
}
}
}
,當涉及到接收免費的酸奶使用IF語句我真的不知道如何使用循環,即舒服,所以,可能是我的問題,因爲我從來沒有提出利用虛假和真實
標題後的建議有人溺水.. –
你檢查過你的'while(!correct)'嗎? –
'if(totalPurchased> 0 || totalPurchased <10){' - 每個數字大於0或小於10.您想'&&'。 – user2357112