Java 试图提示用户从catch块重新输入,但catch块终止?

Java 试图提示用户从catch块重新输入,但catch块终止?,java,exception,exception-handling,try-catch,inputmismatchexception,Java,Exception,Exception Handling,Try Catch,Inputmismatchexception,我正试图编写一个程序,要求用户输入他们的年龄,并在他们输入不正确的值时提示他们重新输入(如负数、大于120、带有特殊字符或字母的年龄、超出范围的数字等) 我尝试写一个try/catch,要求用户重新输入他们的年龄: System.out.println("Enter your age (a positive integer): "); int num; try { num = in.nextInt(); while (num < 0 ||

我正试图编写一个程序,要求用户输入他们的年龄,并在他们输入不正确的值时提示他们重新输入(如负数、大于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();
    }
我的目标是让程序继续提示有效年龄,直到用户获得正确的年龄。 我非常感谢您的反馈和帮助。我是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): ");
        }
    }
while(num<0 | | num>120){
试一试{
System.out.println(“坏年龄。重新输入年龄(正整数):”;
num=in.nextInt();
}捕获(输入不匹配异常e){
System.out.println(“坏年龄。重新输入年龄(正整数):”;
}
}

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


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

因为您试图捕获无效的输入状态,同时仍提示用户输入正确的值,作为验证过程的一部分,
try-catch
应该封装在
循环中

使用
nextLine
读取输入时,无效输入不会被删除,因此您需要确保在尝试使用
nextLine
重新读取之前清除缓冲区。或者您可以放弃它,直接使用
nextLine
读取
字符串
值,然后使用
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);
Scanner Scanner=新的扫描仪(System.in);
int age=-1;
做{
试一试{
System.out.print(“输入0到250年之间的时间:”);
String text=scanner.nextLine();//解决悬空新行
年龄=整数.parseInt(文本);
如果(年龄<0 | |年龄>250){
System.out.println(“无效年龄,必须介于0和250之间”);
}
}捕获(NumberFormatException ime){
System.out.println(“无效输入-请仅输入数字”);
}
}年龄<0 | |年龄>250岁;

使用
do-while
循环,基本上是因为,即使在第一次传递有效值时,您也必须至少迭代一次。相反,将
try catch
块放入
循环中,然后继续循环,直到得到所需的内容need@MadProgrammer你能解释一下你的意思吗?我应该把它放在哪个循环中?@taralee98因为在您的示例代码中只有一个循环,而不是one@MadProgrammer我试过了,但它引起了一个无限循环:(我在上面发布了代码。知道我哪里出错了吗?
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);