2017-02-09 93 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))而不是使用“==” –