我试图让用户无限次输入 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.");
}
对不起,我的理解有限:/