Scanner Class和InputMismatchException

时间:2012-11-19 21:34:59

标签: java exception java.util.scanner

while (!s.hasNextDouble())
{
System.out.println("not a value" );
    s.nextDouble();

}

sum = min = max = next = s.nextDouble();    

for (loop follows to decide min and max and average) 

为什么我在线程“main”java.util.InputMismatchException中获取扫描程序异常,当我运行它时。我查看了API,我认为我做得对。如果不在System.out之后放置s.nextDouble(),那么循环运行正常,但只要键入s.nextDouble();程序崩溃了。

1 个答案:

答案 0 :(得分:3)

您尝试阅读Double而没有人阅读。尝试:

while (!s.hasNextDouble())
{
    System.out.println("not a value" );
    s.next();
}
min = max = etc = s.nextDouble();