2017-02-09 96 views
-2

所以我正在學習Java的學校的程序,我在mac osx上使用Eclipse。當我運行它,它給了我這個錯誤:Java - 錯誤消息InputMismatchException

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.nextInt(Scanner.java:2117) 
at java.util.Scanner.nextInt(Scanner.java:2076) 
at Dank.main(Dank.java:13) 

這是代碼:

   import java.util.Scanner; 
      public class Dank 
      { 
static Scanner in = new Scanner(System.in); 
public static void main(String args[]) 
{ 
int antonio='0'; 
int answer; 
System.out.print("Whats your name? "); 
answer = in.nextInt(); 
if (answer == antonio) { 
    System.out.println("are you sure? you are a Dank Meme "+answer); 
} 
else 
{ 
     System.out.println("Damn, you aren't a Dank Meme "+answer); 
} 
    } 
    } 
+3

你需要在你的問題中包含你的實際代碼,而不是你的代碼圖片的鏈接。 – azurefrog

+2

請發佈必要的代碼以便在此處以純文本形式(** no **圖像,鏈接等)重現問題。 – Paul

+0

只需將「靜態掃描儀...」的行插入主表面,而不是出於主表面,您將永遠不會從此處獲得這樣的代碼,而不是在方法中 – azro

回答

0

的in.nextInt()只允許整數的輸入。所以如果你輸入一個字符串,它會給你inputmismatchexception。嘗試將其更改爲in.nextLine()。你的病情更早。它不會將0讀爲0,程序將其讀作'0'字符,因此它會返回48,因爲您已將其賦給int。嘗試使用此代碼。

public class TestClass { 


static Scanner in = new Scanner(System.in); 
public static void main(String args[]) 
{ 
String antonio = "0"; 
String answer; 

System.out.print("Whats your name? "); 
answer = in.nextLine(); 
if (answer .equalsIgnoreCase(antonio)) { 
System.out.println("are you sure? you are a Dank Meme "+answer); 
} 
else 
{ 
System.out.println("Damn, you aren't a Dank Meme "+answer); 
} 
} 

} 
+0

和至於比較的字符串,如果你需要。 (答案.equalsIgnoreCase(antonio))而不是使用「==」 –