【问题标题】:Trying to prompt the user to re-enter from the catch block, but the catch block terminates?试图提示用户从 catch 块重新进入,但 catch 块终止了?
【发布时间】:2019-02-21 22:27:11
【问题描述】:

我正在尝试编写一个程序来要求用户输入他们的年龄,如果他们输入了不正确的值(例如负数、大于 120、带有特殊字符或字母的年龄、超出范围的数字等...)

我尝试编写一个 try/catch 来要求用户重新输入他们的年龄:

System.out.println("Enter your age (a positive integer): ");
    int num;

    try {
        num = in.nextInt();
        while (num < 0 || num > 120) {
            System.out.println("Bad age. Re-enter your age (a positive integer): ");
            num = in.nextInt();
        }
    } catch (InputMismatchException e) {
        //System.out.println(e);
        System.out.println("Bad age. Re-enter your age (a positive integer): ");
        num = in.nextInt();
    }

当输入的年龄包含特殊字符/字母或超出范围时,程序会打印出“Bad age. Re-enter your age (a positive integer)”字样,但随后会立即终止并出现以下错误:

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Unknown Source)
at java.base/java.util.Scanner.next(Unknown Source)
at java.base/java.util.Scanner.nextInt(Unknown Source)
at java.base/java.util.Scanner.nextInt(Unknown Source)
at Age.main(Age.java:21)

我的目标是让程序继续提示输入有效年龄,直到用户正确为止。 我非常感谢任何反馈和帮助。我是一个java初学者:) 谢谢

我尝试将整个代码更改为 while 循环,但随后导致无限循环...请帮助!

while (num < 0 || num > 120) {
        try {
            System.out.println("Bad age. Re-enter your age (a positive integer): ");
            num = in.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Bad age. Re-enter your age (a positive integer): ");
        }
    }

【问题讨论】:

  • 不要。而是将try-catch 块放在loop 中,然后继续循环直到你得到你需要的东西
  • @MadProgrammer 你能解释一下你的意思吗?我应该把它放在哪个循环中?
  • @taralee98 由于您的示例代码中只有一个循环,而不是那个
  • @MadProgrammer 我试过了,但它导致了一个无限循环:(我发布了上面的代码。知道我哪里出错了吗?

标签: java exception exception-handling try-catch inputmismatchexception


【解决方案1】:

由于您尝试捕获无效输入状态,同时仍提示用户输入正确值,因此应将 try-catch 封装在 loop 中,作为其验证过程的一部分。

使用nextInt 读取输入时,不会删除无效输入,因此您需要确保在尝试使用nextLine 重新读取之前清除缓冲区。或者你可以放弃它,直接使用nextLine 读取String 的值,然后使用Integer.parseInt 将其转换为int,这对个人而言比较不麻烦。

Scanner scanner = new Scanner(System.in);
int age = -1;
do {
    try {
        System.out.print("Enter ago between 0 and 250 years: ");
        String text = scanner.nextLine(); // Solves dangling new line
        age = Integer.parseInt(text);
        if (age < 0 || age > 250) {
            System.out.println("Invalid age, must be between 0 and 250");
        }
    } catch (NumberFormatException ime) {
        System.out.println("Invalid input - numbers only please");
    }
} while (age < 0 || age > 250);

使用do-while 循环,基本上是因为,即使是第一次传递的有效值,您也必须至少迭代一次

【讨论】:

    【解决方案2】:

    即使您能够提示用户重新输入他的年龄,您也无法在之后检查输入是否正确。因此,我建议使用一个简单的 while 循环,就像你正在做的那样,但不要只寻找一个数字范围,而是在尝试用 int 解析它之前检查它是否是一个数字。

    如果你使用 input.nextLine().trim();例如,您可以使用诸如 StringUtils.isNumeric 之类的方法,或者您可以实现自己的方法来返回一个布尔值,指示输入是否为数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      相关资源
      最近更新 更多