This question already has answers here:
nextDouble() throws an InputMismatchException when I enter a double
                                
                                    (2个答案)
                                
                        
                        
                            InputMismatchException using Scanner#nextDouble with valid values
                                
                                    (2个答案)
                                
                        
                24天前关闭。
            
        

Scanner scan = new Scanner(System.in);
        double cost = scan.nextDouble();
        System.out.println(cost);


所以我尝试编写7.6,并且它在main中引发了异常。但是当我写7,6就可以了,它可以打印7.6

我正在使用eclipse作为我的IDE

最佳答案

系统区域设置使用小数点分隔符','(在当前系统上),您可以明确指定扫描仪应使用的区域设置,例如

Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);
double cost = scan.nextDouble();

10-06 03:36