为什么我会收到 InputMismatchException? [英] Why am I getting InputMismatchException?
问题描述
到目前为止我有这个:
public double checkValueWithin(int min, int max) {
double num;
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();
while (num < min || num > max) {
System.out.print("Invalid. Re-enter number: ");
num = reader.nextDouble();
}
return num;
}
还有这个:
public void askForMarks() {
double marks[] = new double[student];
int index = 0;
Scanner reader = new Scanner(System.in);
while (index < student) {
System.out.print("Please enter a mark (0..30): ");
marks[index] = (double) checkValueWithin(0, 30);
index++;
}
}
当我测试这个时,它不能使用双号,我收到了这条消息:
When I test this, it can't take double number and I got this message:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at MarkingSystem.checkValueWithin(MarkingSystem.java:25)
at MarkingSystem.askForMarks(MarkingSystem.java:44)
at World.main(World.java:6)
Java Result: 1
我该如何解决这个问题?
How do I fix this?
推荐答案
在这里你可以看到 扫描仪:
double nextDouble()
double nextDouble()
以双精度形式返回下一个标记.如果下一个令牌不是浮点数或超出范围,抛出 InputMismatchException.
Returns the next token as a double. If the next token is not a float or is out of range, InputMismatchException is thrown.
尝试捕获异常
try {
// ...
} catch (InputMismatchException e) {
System.out.print(e.getMessage()); //try to find out specific reason.
}
更新
案例 1
我试过你的代码,没有任何问题.您收到该错误是因为您必须输入 String
值.当我输入一个数值时,它运行没有任何错误.但是一旦我输入 String
它 throw
与您在问题中提到的相同 Exception
.
I tried your code and there is nothing wrong with it. Your are getting that error because you must have entered String
value. When I entered a numeric value, it runs without any errors. But once I entered String
it throw
the same Exception
which you have mentioned in your question.
案例 2
您输入了一些超出范围的内容,正如我上面提到的.
You have entered something, which is out of range as I have mentioned above.
我真的很想知道您本可以尝试输入什么内容.在我的系统中,它运行完美,无需更改一行代码.只需按原样复制并尝试编译和运行它.
I'm really wondering what you could have tried to enter. In my system, it is running perfectly without changing a single line of code. Just copy as it is and try to compile and run it.
import java.util.*;
public class Test {
public static void main(String... args) {
new Test().askForMarks(5);
}
public void askForMarks(int student) {
double marks[] = new double[student];
int index = 0;
Scanner reader = new Scanner(System.in);
while (index < student) {
System.out.print("Please enter a mark (0..30): ");
marks[index] = (double) checkValueWithin(0, 30);
index++;
}
}
public double checkValueWithin(int min, int max) {
double num;
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();
while (num < min || num > max) {
System.out.print("Invalid. Re-enter number: ");
num = reader.nextDouble();
}
return num;
}
}
如您所说,您尝试输入1.0
、2.8
等,请尝试输入此代码.
As you said, you have tried to enter 1.0
, 2.8
and etc. Please try with this code.
注意:请在单独的行中一一输入数字.我的意思是,输入 2.7
,按 Enter,然后输入第二个数字(例如 6.7
).
Note : Please enter number one by one, on separate lines. I mean, enter 2.7
, press enter and then enter second number (e.g. 6.7
).
这篇关于为什么我会收到 InputMismatchException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!