我尝试以最简单的方式使用扫描仪:
码:
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中的状态:
由扫描程序抛出,表示检索到的令牌
与预期类型的模式不匹配,或者令牌超出预期类型的范围.
但在我看来,所有看起来都正确,并且应该工作正常.
问题: