我使用的是Windows 7机器,其"控制面板\时钟,语言和地区"是"丹麦"
根据Scanner的文档:
扫描程序的初始语言环境是Locale.getDefault()方法返回的值;
但是当我运行代码时:
System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();
Run Code Online (Sandbox Code Playgroud)
它输出"en_US"然后在sc.nextDouble()处抛出java.util.InputMismatchException.扫描仪初始化为"1,0"时有效
但是,如果我明确设置了Locale:
Locale.setDefault(Locale.US);
System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();
Run Code Online (Sandbox Code Playgroud)
它输出"en_US"然后解析双精度就好了.我错过了什么,或扫描仪的文档是错误的?
编辑根据@Perception的建议,我在第一个例子中查看了sc.locale().它打印"da_DK".那么为什么它不是"en_US",那么Locale.getDefault()方法返回的是什么?