本文介绍了使用带有效值的Scanner#nextDouble的InputMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的扫描仪代码:
Here is my code for my scanner:
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)
我该怎么做才能删除该错误?
What can I do to remove that error?
推荐答案
这是一个区域设置问题:
如果你想使用。你可以尝试类似的东西:
It's a locale issue : If you want to use . you can try something like :
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);
}
}
这篇关于使用带有效值的Scanner#nextDouble的InputMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!