【发布时间】:2015-01-17 15:26:21
【问题描述】:
我已经搜索过,但我真的找不到代码有什么问题,请帮忙!
代码可以编译,但这是我想回答问题 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();
}
}
【问题讨论】:
-
尝试在
nextDouble()之前添加一个nextLine()调用 -
您是在德国还是在使用
,而不是.输入浮点数的地方?尝试回答0,5(对我有用) -
@zapl 谢谢!是的,我在德国!
标签: java inputmismatchexception