为什么我得到错误“异常线程‘main’java.util.InputMismatchException”
我得到了一个简单的任务来完成,建立一个分级标准,这将使学生对所提交值的字母等级。除了学生完成之后,一切似乎都能顺利运行 - 他们需要输入“E”才能退出程序......这是我丢失的地方,因为我收到了这个异常"Exception in thread "main" java.util.InputMismatchException"
。我知道结局是小东西,我很想念,但我不能弄明白这里是我目前拥有的代码:为什么我得到错误“异常线程‘main’java.util.InputMismatchException”
public static void main(String[] args) {
int letterGrade;//The overall letter grade
boolean prompt = true;
while (prompt) {
//Prompt the student to input their data
System.out.print("Please enter your exam score, or press E to exit :");
@SuppressWarnings("resource")
Scanner keyboard = new Scanner(System.in);
letterGrade = keyboard.nextInt();
if ((letterGrade >= 90) & (letterGrade <= 100)) {
System.out.println("The letter grade is A");
} else if ((letterGrade >= 80) & (letterGrade <= 89)) {
System.out.println("The letter grade is B");
} else if ((letterGrade >= 70) & (letterGrade <= 79)) {
System.out.println("The letter grade is C");
} else if ((letterGrade >= 60) & (letterGrade <= 69)) {
System.out.println("The letter grade is D");
} else if ((letterGrade >= 0) & (letterGrade <= 59)) {
System.out.println("The letter grade is F");
} else {
System.out.println("Invalid input, try again.");
}
}
String firstChar = "e";
String secondChar = "E";
{
if ((firstChar == secondChar)) {
System.out.println("Thank you for using the grading system");
} else {
System.out.println("Thank you for using the grading system");
}
{
}
}
{
}
}
}
我在想什么?我已经呆了两周了?
主要问题是采取一切输入为INT没有int或不检查,因为“E”是可能的输入。 如果你想用户输入"E" or "e"
你应该首先检查是否是用户输入值数字或不是。你可以使用try{catch}
但很好用regex
。并比较字符串不正确的代码。你可以使用||
来检查用户输入e或E但.tolowercase或equalsIgnoreCase
简单
public class NewClass {
public static void main(String[] args) {
String firstChar = "e";
String secondChar = "E";
boolean prompt = true;
while (prompt) {
//Prompt the student to input their data
System.out.print("Please enter your exam score, or press E to exit :");
@SuppressWarnings("resource")
Scanner keyboard = new Scanner(System.in);
String userinput = keyboard.next();
if(isNumeric(userinput)){
int letterGrade=Integer.parseInt(userinput);
if ((letterGrade >= 90) & (letterGrade <= 100)) {
System.out.println("The letter grade is A");
} else if ((letterGrade >= 80) & (letterGrade <= 89)) {
System.out.println("The letter grade is B");
} else if ((letterGrade >= 70) & (letterGrade <= 79)) {
System.out.println("The letter grade is C");
} else if ((letterGrade >= 60) & (letterGrade <= 69)) {
System.out.println("The letter grade is D");
} else if ((letterGrade >= 0) & (letterGrade <= 59)) {
System.out.println("The letter grade is F");
} else {
System.out.println("Invalid input, try again.");
}
}else if(userinput.equalsIgnoreCase(firstChar)){
System.out.println("Thank you for using the grading system");
System.exit(0);
}
}
}
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
}
,如果你不能使用这种方法equalsignorecase
可以使用or
操作
else if(userinput.equals(firstChar)||userinput.equals(secondChar)){
System.out.println("Thank you for using the grading system");
System.exit(0);
}
而且,如果你不能使用regex
.replace ISNUMERIC方法与以下 尝试捕捉逻辑
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
学会使用不会发生'.equalsIgnoreCase()'是更有效和测试平等不区分大小写的惯用方式!它也正确使用'i18n'! – 2014-09-30 01:27:46
@JarrodRoberson OK。我改变tolowercase到equalsIgnoreCase – 2014-09-30 01:31:30
@JarrodRoberson主要问题是采取一切输入为INT不检查int或不是因为“E”可以input.this是正确的做法对于这个疑难问题了解equalsIgnoreCase绝对有效在这个案件。 – 2014-09-30 01:56:17
“E” 不是一个'int'。 – 2014-09-30 01:00:41
当我不使用[int]时,我怎样才能收到字母等级并退出带有“E”的程序?字母等级与输入值不匹配。 – 2014-09-30 01:05:36
也'firstChar == secondChar'不会做你认为它... – John3136 2014-09-30 01:10:40