nextDouble() 不接受点,只接受逗号

nextDouble() don't accept points, just commas

输入是这样的:

185.50
250.36
163.45
535.20
950.22
450.38
-1

它给我带来了这个:

Exception in thread "main" java.util.InputMismatchException<br>
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)<br>
    at java.base/java.util.Scanner.next(Scanner.java:1594)<br>
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)<br>
    at Pruebas.Ventas.main(Ventas.java:25)

但是如果我输入这个:

185,50
250,36
163,45
535,20
950,22
450,38
-1

效果很好!为什么?

import java.util.Scanner;

public class Ventas {

    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);

        double numero = 0;
        double grande = 0;
        double pequeno = 0;
        double suma = 0;
        double media = 0;
        String comprobacion = "";
        int grandePos = 0;
        int pequenoPos = 0;
        String[] semana = {"Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"};

        while(numero != -1) {
            double[] dias = new double[6];
            for (int i = 0; i < dias.length; i++) {
                numero = scan.nextDouble(); // This is line 25!
                suma += numero;
                if(numero != -1) {
                    dias[i] = numero;
                    if(numero > grande) {
                        grande = numero;
                        grandePos = i;
                    }else if(pequeno == 0 || numero < pequeno) {
                        pequeno = numero;
                        pequenoPos = i;
                    }
                }else {
                    break;
                }
            }
            media = ((suma - dias[5]) / 5);
            if(media < dias[5]) {
                comprobacion = "NO";
            }else {
                comprobacion = "SI";
            }
        }
        
        System.out.println(semana[grandePos] + " " + semana[pequenoPos] + " " + comprobacion);

        scan.close();
    }

}

您的默认语言环境使用逗号字符 (,) 作为小数点分隔符,因此无法将带小数点 (.) 的输入解析为双精度数。您可以通过显式设置接受小数点的 Locale 来覆盖此行为。例如:

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