相关疑难解决方法(0)

扫描仪双值 - InputMismatchException

我尝试以最简单的方式使用扫描仪:

码:

double gas, efficiency, distance, cost;
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of gallons of gas in the tank: ");
gas = scanner.nextDouble();
System.out.print("Enter the fuel efficiency: ");
efficiency = scanner.nextDouble();
Run Code Online (Sandbox Code Playgroud)

但在第一次输入后5.1它抛出:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at udacity.MileagePrinter.main(MileagePrinter.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)

JavaDoc中的状态:

由扫描程序抛出,表示检索到的令牌
与预期类型的模式不匹配,或者令牌超出预期类型的​​范围.

但在我看来,所有看起来都正确,并且应该工作正常.

问题:

  • 为什么会出现这种情况?
  • 如何规避这个麻烦?

java inputmismatchexception

21
推荐指数
2
解决办法
3万
查看次数

标签 统计

inputmismatchexception ×1

java ×1