2017-02-05 14 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

相關問題