System.out.println("How long is the word you would like to guess?");
while (goodInput==false)
{
try
{
wordSize=scan.nextInt();
goodInput=true;
}
catch(InputMismatchException ime)
{
System.out.println("Thats not a number! Try again");
}
}
输入错误类型的输入后,控制台在无限循环中重复“那不是数字...”。
*编辑
我试过了
while(goodInput==false)
{
if (scan.hasNextInt())
{
wordSize=scan.nextInt();
goodInput=true;
}
else
{
System.out.println("Thats not a number! Try again");
}
}
这也会产生相同的错误