线程" main"中的例外情况java.util.InputMismatchException帮帮我吧?

时间:2017-03-14 12:54:52

标签: java

我的代码:

public class JavaApplication3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner keyboard = new Scanner(System.in);

        System.out.println( "What city is the capital of France?" );
        keyboard.next();

        System.out.println( "What is 6 multiplied by 7?" );
        keyboard.nextInt();

        System.out.println( "Enter a number between 0.0 and 1.0." );
        keyboard.nextDouble();

        System.out.println( "Is there anything else you would like to say?" );
        keyboard.next();
    }

}

我在问题之前运行正常输入介于0.0和1.0之间的数字。它的错误是这样的:

***What city is the capital of France?

Paris

What is 6 multiplied by 7?

442

Enter a number between 0.0 and 1.0.

0.5

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:28)
C:\Users\Administrator\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 7 seconds)***

1 个答案:

答案 0 :(得分:3)

如果条目的格式对于扫描程序的区域设置不正确,

扫描程序将抛出此异常。特别是,在您的情况下,如果使用错误的小数分隔符。两者。并且,是常见的特定于语言环境的小数分隔符。

要找出您的默认语言环境的小数分隔符,您可以使用:

System.out.println(
    java.text.DecimalFormatSymbols.getInstance().getDecimalSeparator()
);

在"原创"中查看更多内容主题here