java提示用户输入正确的输入类型
while (goodInput=false)
{
try
{
System.out.println("How long is the word you would like to guess?");
wordSize=scan.nextInt();
while(wordSize>word.longestWord())
{
System.out.println("There are no words that big! Please enter another number");
wordSize=scan.nextInt();
}
goodInput=true;
}
catch(InputMismatchException ime)
{
System.out.println("Thats not a number! Try again");
}
}
我试图提示用户输入一个数字,但我无法让它正常运行。我希望它一直运行,直到输入正确的输入
# 1 楼答案
首先,
在将
false
赋值给goodInput
时,必须使用==
运算符检查goodInput
是否为false
或者只是
以下是java中对Equality Operator的引用
# 2 楼答案
while
循环中的条件需要您所做的是将
false
分配给goodinput,最终结果为false
。请参见以下语句的输出你需要一个equality operator在那里
# 3 楼答案
你必须写作
甚至更好
而不是
第一个将
goodInput
的值与false
进行比较,第二个将goodInput
的值取反,您的版本将false
分配给goodInput
# 4 楼答案
一个问题是:
将
false
分配给goodInput
,使之成为while(false)
,从而导致循环根本不执行换成