Java 为什么我得到输入不匹配异常?
到目前为止,我有:Java 为什么我得到输入不匹配异常?,java,input,java.util.scanner,inputmismatchexception,Java,Input,Java.util.scanner,Inputmismatchexception,到目前为止,我有: public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num < min || num > max) { Syst
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;
}
如何解决此问题?由于您有手动用户输入循环,扫描仪读取您的第一个输入后,它将通过托架/返回到下一行,该行也将被读取;当然,这不是你想要的 你可以试试这个
try {
// ...
} catch (InputMismatchException e) {
reader.next();
}
或者,您也可以通过调用
reader.next()
在这里,您可以看到: double nextDouble() 以双精度形式返回下一个标记如果下一个标记不是浮点或 超出范围,将抛出InputMismatchException。 尝试捕捉异常
try {
// ...
} catch (InputMismatchException e) {
System.out.print(e.getMessage()); //try to find out specific reason.
}
更新
案例1
我试过你的代码,没有问题。您收到该错误是因为您必须输入字符串值。当我输入一个数值时,它运行时没有任何错误。但是一旦我输入了String
itthrow
您在问题中提到的异常
案例2
您输入了超出范围的内容,如上所述
我真的很想知道你能试着进入什么地方。在我的系统中,它在不更改任何一行代码的情况下完美地运行。只需按原样复制,并尝试编译和运行它
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;
}
}
import java.util.*;
公开课考试{
公共静态void main(字符串…参数){
新测试().askForMarks(5);
}
公共无效ASK格式(国际学生){
双倍分数[]=新的双倍[学生];
int指数=0;
扫描仪阅读器=新扫描仪(System.in);
while(指数<学生){
系统输出打印(“请输入一个标记(0..30):”;
在(0,30)范围内标记[索引]=(双重)校验值;
索引++;
}
}
公共双校验值(最小整数、最大整数){
双数;
扫描仪阅读器=新扫描仪(System.in);
num=reader.nextDouble();
而(nummax){
系统输出打印(“无效。重新输入号码:”);
num=reader.nextDouble();
}
返回num;
}
}
正如您所说,您已尝试输入1.0
、2.8
等。请使用此代码进行尝试
注意:请在单独的行中逐个输入数字。我的意思是,输入2.7
,按enter键,然后输入第二个数字(例如6.7
)。您是否向控制台提供写入输入
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();
如果您只需输入456之类的数字,则返回双精度。
如果您输入字符串或字符,当它尝试执行num=reader.nextDouble()时,它将抛出java.util.InputMismatchException。我遇到了同样的问题。
奇怪,但原因是物体扫描仪根据系统的定位来解释分数。
如果当前本地化使用逗号分隔部分分数,则带点的分数将变成字符串类型。
因此出现了错误…请不要使用点,如:1.2,试着这样输入:1,2。看看这个。也许扫描器无法解析您在控制台中输入的内容?例如,它要求输入一个数字,您输入了“hello”?请尝试改为执行“nextFloat”。Remove Scanner reader=new Scanner(System.in);来自askForMarks();然后一切都对我有效。@transtrung检查我的更新。就像我说的,我只输入双精度值,如1.0、5.8等,但仍然显示相同的值message@TrầnTrung复制整个代码,编译并运行它。并且,尝试提供一些您之前尝试过的输入。另外,请检查我最后一个答案的注释。还有,请告诉我。如果你找到了解决方案,那么也请告诉我。我想我找到了问题所在,因为我的计算机只识别“,”和小数部分,而我使用的输入与“.”一起,这就是它无法工作的原因。试着更改扫描仪的区域设置:。useLocale(locale.ENGLISH)
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;
}
}
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();