我已经搜索过,但我确实找不到代码中的任何错误,请帮忙!
代码可以编译,但是,这是我想回答问题 3 时遇到的错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at ForgetfulMachine.main(ForgetfulMachine.java:16)
这是我的代码:
import java.util.Scanner;
public class ForgetfulMachine
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println( "What city is the capital of Germany?" );
keyboard.next();
System.out.println( "What is 6 divided by 2?" );
keyboard.nextInt();
System.out.println( "What is your favorite number between 0.0 and 1.0?" );
keyboard.nextDouble();
System.out.println( "Is there anything else you would like to tell me?" );
keyboard.next();
}
}
最佳答案
如果条目的格式对于扫描程序的区域设置不正确,
扫描程序
将引发此异常。特别是,在您的情况下,如果使用了错误的小数分隔符。 .
和 ,
都是常见的区域特定小数分隔符。
要了解您的默认区域设置的小数分隔符是什么,您可以使用:
System.out.println(
javax.text.DecimalFormatSymbols.getInstance().getDecimalSeparator()
);
另请参阅:
关于java - "Exception in thread "主要 "java.util.InputMismatchException"**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028230/