线程“main”中的异常 java.util.InputMismatchException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29313544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:02:17  来源:igfitidea点击:

Exception in thread "main" java.util.InputMismatchException

javajava.util.scannerinputmismatchexception

提问by ???

I`m MAC user and I have been struggling with this error. I search this error, now I just can't find solutions.

我是 MAC 用户,我一直在为这个错误而苦苦挣扎。我搜索这个错误,现在我找不到解决方案。

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 GradeBook.inputGrades(GradeBook.java:53)
at GradeBookTest.main(GradeBookTest.java:12)

It is Not a problem of the variable, and the code written As recorded in the book .

不是变量的问题,和书上写的代码一样。

回答by Touchstone

Check the following thing and also check whether you are respecting the rules:

检查以下事项并检查您是否遵守规则:

public int nextInt()

Throws:InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

抛出:InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围

For more details about nextmethod visit:

有关下一个方法的更多详细信息,请访问:

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

回答by Shaik Mohammed Saifullah

If you are using Switch case in the above program then give a space between switch and condition.

如果您在上述程序中使用 Switch case,则在 switch 和 condition 之间留一个空格。

Dont write like this --> switch(n) Write like this --> switch (n)

不要这样写 --> switch(n) 这样写 --> switch(n)