这是我的扫描仪代码:
import java.util.Scanner; public class TryDouble { public static void main(String [] args){ Scanner jin = new Scanner(System.in); double a = jin.nextDouble(); double b = jin.nextDouble(); double c = jin.nextDouble(); System.out.println(a + b + c); } }
输入:0.2 0.5 0.9,我得到:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at TryDouble.main(TryDouble.java:6)
我该怎么做才能消除该错误?
这是一个区域设置问题:如果要使用。您可以尝试类似:
package test; import java.util.Locale; import java.util.Scanner; public class TryDouble { public static void main(String [] args){ Scanner jin = new Scanner(System.in).useLocale(Locale.US); double a = jin.nextDouble(); double b = jin.nextDouble(); double c = jin.nextDouble(); System.out.println(a + b + c); } }