0
我試圖讓用戶在0到10之間輸入任意數量的無限次數,直到他們想要停止。他們通過輸入值-1來停止。到目前爲止,我已經能夠創建當他們輸入正確的值時發生了什麼,但是當他們輸入-1(在while循環中這是一個無效值)時,程序知道它是無效的。我要找的所有程序都是爲了排除可能的無效輸入而排除-1,並使程序停止詢問更多輸入。這裏是我到目前爲止的代碼:如何獲取用戶輸入的數字以退出循環?
int userInput=0;
System.out.println("Please enter numbers ranging from 0 to 10 (all inclusive).");
System.out.println("When you want to stop, type and enter -1.");
while (userInput <= 10 && userInput >= 0)
{
userInput=Integer.parseInt(br.readLine());
while (userInput > 10|| userInput < 0)
{
System.out.println("That number is not in between 0 and 10. Please enter a correct number.");
userInput=Integer.parseInt(br.readLine());
}
sum=sum+userInput;
freq++;
}
while (userInput == -1)
{
System.out.println("You have chosen to stop inputing numbers.");
}
對不起,我有限的瞭解:/