Java: 输入两个错误值后程序崩溃
Java: Program crashes after inputting two bad values
我编写了一个程序,计算 gcd(最大公因数)和 lcm(最小公倍数)。除 try {...} catch(...) {...}
外,一切正常。这是代码中无法按我的意愿工作的部分:
try {
num1 = Integer.parseInt(sc.nextLine());
}
catch(Exception e) {
System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
System.out.print("Enter your first number: ");
num1 = Integer.parseInt(sc.nextLine());
}
当我输入例如字母,它说:
Your input is not an integer (number w/o decimals)! Try again.
Enter your first number:
但是当我第二次输入字母时,程序崩溃了:
Exception in thread "main" java.lang.NumberFormatException: For input string: "asdf"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at GCDLCMgetter.main(GCDLCMgetter.java:56)
这可能是我犯的一个非常简单的错误,但我想不通...
谢谢
当你第一次给出字母时,它会进入 catch
块。显示错误信息。然后执行行 num1 = Integer.parseInt(sc.nextLine());
您再次输入了字母,但这次没有 try-catch
块来处理这个问题。所以它会抛出错误。
这是因为您的第二个提示在 catch 块内。您不想在 catch 块中再次提示,而是希望将整个代码部分包装在一个循环中,以便它再次返回到 try 块以进行提示。
类似于:
boolean repeat = true;
while(repeat){
try{
//Prompt for input
repeat = false;
}catch(Exception e) {
//Display error message
}
}
您的第二个 parseInt 方法调用不在 try catch 块中。你需要为这种逻辑使用循环。
在您的代码中,它执行了两次:
- 在
try{...}
读取一行
- 有个
Exception
- 异常由
catch(Exception e){...}
处理
- 无法处理
catch(Exception e){...}
里面的num1 = Integer.parseInt(sc.nextLine());
。它没有放在 try{}
. 里面
- 执行完成,因为最后一个异常无法被任何
catch
处理。
您似乎正在使用 Scanner,我建议您使用循环方式:
while (sc.hasNextLine()){
try {
System.out.print("Enter your first number: ");
num1 = Integer.parseInt(sc.nextLine());
}
catch(Exception e) {
System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
}
}
如果您要管理整数,使用 Scanner.nextInt()
会很有趣
while (sc.hasNextInt()){
try {
System.out.print("Enter your first number: ");
num1 = sc.nextInt());
}
catch(Exception e) {
System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
}
}
我编写了一个程序,计算 gcd(最大公因数)和 lcm(最小公倍数)。除 try {...} catch(...) {...}
外,一切正常。这是代码中无法按我的意愿工作的部分:
try {
num1 = Integer.parseInt(sc.nextLine());
}
catch(Exception e) {
System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
System.out.print("Enter your first number: ");
num1 = Integer.parseInt(sc.nextLine());
}
当我输入例如字母,它说:
Your input is not an integer (number w/o decimals)! Try again.
Enter your first number:
但是当我第二次输入字母时,程序崩溃了:
Exception in thread "main" java.lang.NumberFormatException: For input string: "asdf"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at GCDLCMgetter.main(GCDLCMgetter.java:56)
这可能是我犯的一个非常简单的错误,但我想不通...
谢谢
当你第一次给出字母时,它会进入 catch
块。显示错误信息。然后执行行 num1 = Integer.parseInt(sc.nextLine());
您再次输入了字母,但这次没有 try-catch
块来处理这个问题。所以它会抛出错误。
这是因为您的第二个提示在 catch 块内。您不想在 catch 块中再次提示,而是希望将整个代码部分包装在一个循环中,以便它再次返回到 try 块以进行提示。 类似于:
boolean repeat = true;
while(repeat){
try{
//Prompt for input
repeat = false;
}catch(Exception e) {
//Display error message
}
}
您的第二个 parseInt 方法调用不在 try catch 块中。你需要为这种逻辑使用循环。
在您的代码中,它执行了两次:
- 在
try{...}
读取一行
- 有个
Exception
- 异常由
catch(Exception e){...}
处理
- 无法处理
catch(Exception e){...}
里面的num1 = Integer.parseInt(sc.nextLine());
。它没有放在try{}
. 里面
- 执行完成,因为最后一个异常无法被任何
catch
处理。
您似乎正在使用 Scanner,我建议您使用循环方式:
while (sc.hasNextLine()){
try {
System.out.print("Enter your first number: ");
num1 = Integer.parseInt(sc.nextLine());
}
catch(Exception e) {
System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
}
}
如果您要管理整数,使用 Scanner.nextInt()
会很有趣while (sc.hasNextInt()){
try {
System.out.print("Enter your first number: ");
num1 = sc.nextInt());
}
catch(Exception e) {
System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
}
}