捕获InputMissMatchException后继续while循环,Java

6ljaweal  于 2023-11-15  发布在  Java
关注(0)|答案(3)|浏览(111)

我是Java新手,在捕获异常后遇到了问题。捕获异常后程序结束,但我想继续循环。程序的使用是用户通过扫描器输入1到10之间的数字,直到输入的数字等于之前定义的随机数。

public class GuessingGame {

    public static void main(String[] args) {

        System.out.println("Welcome!");

        int min = 1;
        int max = 1000;

        int randomNumber = (int) (Math.random() * (max - min) + 1) + min;

        Scanner scanner = new Scanner(System.in);

        int firstDraw = 1;
        int maxDraw = 1000;

        try {

            while (firstDraw < maxDraw) {

                System.out.println("Please enter your guess: ");

                int guessingNumber = scanner.nextInt();

                if (guessingNumber < randomNumber) {
                    System.out.println("Too small!");
                } else if (guessingNumber > randomNumber) {
                    System.out.println("Too big!");
                } else {
                    System.out.println("You win");
                }
                firstDraw++;
            }
        } catch (InputMismatchException e){
            System.out.println("Not a number - try again!");
        }
    }
}

字符串
我想问题是,我将把try-catch块放在哪里。我尝试了几种可能性,但我没有得到正确的解决方案。如果我只是输入数字,程序工作,直到我猜到正确的数字。但如果我使用字母而不是数字,它结束了程序。我也看到过一些类似的问题,但我真的不明白
谢谢你的帮助:)

lx0bsm1f

lx0bsm1f1#

这可能是你想要的吗?

public static void main(String[] args) {

    System.out.println("Welcome!");

    int min = 1;
    int max = 1000;

    int randomNumber = (int) (Math.random() * (max - min) + 1) + min;

    int firstDraw = 1;
    int maxDraw = 1000;

        while (firstDraw < maxDraw) {

            System.out.println("Please enter your guess: ");

            try {
                Scanner scanner = new Scanner(System.in);
                int guessingNumber = scanner.nextInt();

                if (guessingNumber < randomNumber) {
                    System.out.println("Too small!");
                } else if (guessingNumber > randomNumber) {
                    System.out.println("Too big!");
                } else {
                    System.out.println("You win");
                    break;
                }
                firstDraw++;

            } catch (InputMismatchException e){
                System.out.println("Not a number - try again!");
            }

        }

}

字符串

soat7uwm

soat7uwm2#

我理解你的问题:

  1. try-catch需要在while-loop中,而不是相反。
  2. scanner.nextInt()需要一个scanner.nextLine(),就像这样:click to see code example
    1.你需要一个scanner。catch语句中的nextLine():click to see code example
ih99xse1

ih99xse13#

  • "...捕获异常后程序结束,但我想继续循环..."*

使用 Scanner#next 和 * Scanner #parseInt* 方法。
然后,将 try-catch 放在 loop 中。
此外,当猜测到正确的值时,break the loop
注意,parseInt 方法抛出的是 * NumberException *,而不是 InputMismatchException

while (firstDraw < maxDraw) {

    System.out.println("Please enter your guess: ");

    try {
        int guessingNumber = Integer.parseInt(scanner.next());

        if (guessingNumber < randomNumber) {
            System.out.println("Too small!");
        } else if (guessingNumber > randomNumber) {
            System.out.println("Too big!");
        } else {
            System.out.println("You win");
            break;
        }
        firstDraw++;
    } catch (NumberFormatException e){
        System.out.println("Not a number - try again!");
    }
}

字符串
输出

Welcome!
Please enter your guess: 
abc
Not a number - try again!
Please enter your guess: 
123
Too small!
Please enter your guess: 
456
Too big!
Please enter your guess: 
241
You win


最后,您可以使用 RandomGenerator#nextInt 方法来指定边界。

int randomNumber = new Random().nextInt(min, max + 1);

相关问题