2017-02-05 70 views
0

首先,我在Java中竞争noob。我试图获得用户输入。 Eclipse中显示了这个错误,每当我尝试键入双变量:线程“main”中的InputMismatchException

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 lesson1.MyClass.main(MyClass.java:10) 

这是我到目前为止的代码:

import java.util.Scanner; 

public class MyClass { 

public static void main(String[] args) { 
Scanner scan1 = new Scanner(System.in); 
System.out.println("Enter some decimal value"); 
double userInput = scan1.nextDouble(); 
System.out.println("The entered value is "); 
System.out.print(userInput); 
} 
+1

好像有什么noticably错误的代码,它可能是扫描仪不能从你的输入解析双。尝试输入一个数字,然后输入文字并比较结果 – Nic

+1

你究竟输入了什么? –

+0

在控制台中,写如11.111之类的数字值,如果你写了String,那么它会抛出这样的错误。 – yash

回答

0

上面的代码是对的! 您必须确保输入小数,如12.3 下面是我的测试: Test Result

相关问题