我正在使用一台 Windows 7 计算机,其“控制面板\时钟、语言和区域”设置为“丹麦”。
根据Scanner的文档:
扫描仪的初始区域设置是由 Locale.getDefault() 方法返回的值;
但是当我运行代码时:
System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();
它会输出“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();
它输出"en_US",然后成功解析双精度。我有所遗漏吗?还是Scanner文档有误?
编辑:根据@Perception的建议,我查看了第一个示例中的sc.locale()。它打印出"da_DK"。那么当Locale.getDefault()方法返回"en_US"时,为什么不是"en_US"呢?
en_US
呢?但是 @AndrewMao 一定是对的,它对我有效。 - Sotirios Delimanolissc.locale()
会打印什么? - Perception