【发布时间】:2012-12-09 17:54:49
【问题描述】:
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");
}
}
也会产生同样的错误
【问题讨论】:
-
你应该使用
scan.hasNextInt()方法而不是捕获异常。 -
wordSize的类型是什么?
-
@Fyre 假设代码可以编译,它只能是
int或Integer -
您可能需要在 .nextint 之后使用 .nextline 方法读取下一行字符
标签: java exception-handling java.util.scanner