首页 > 解决方案 > 即使使用 try 和 catch 语句,程序也会崩溃

问题描述

我注意到的一个问题是,如果您输入非数字,即使有一个 catch 语句,程序也会崩溃。有人可以帮我吗?我似乎找不到问题所在。

你能否解释一下我做错了什么以及为什么错了?先感谢您。

//在我创建新的扫描仪时,之前的代码有问题。现在是正确的。

import java.util.*;

public class HighLow {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int numberOfAttempts = 0;

    System.out.println("WIN THE THREE ROUNDS TO WIN THE GAME!.");

    int theNumber = (int) (Math.random() * 10 + 1);

    System.out.println("Round 1: I am thinking of a number between" + "\n" + "1 and 10, can you guess the number in " + "\n" + "less than 3 attempts?");    

    System.out.println("Type a number below and press enter.");

    int guess = 0;

    do {

      try {

        guess = scan.nextInt();

        if (guess != theNumber) {

          if (guess < theNumber) {

            System.out.println("Sorry " + guess + " is too low, please try again");

          }else if (guess > theNumber) {

            System.out.println("Sorry " + guess + " is too high, please try again." );

          }
          numberOfAttempts = numberOfAttempts + 1;

        }else {
          System.out.println(guess + " is the correct answer. You got it on" + "\n" + "attempt number " + (numberOfAttempts  + 1)  + ". Proceeding to round 2.");

          theNumber = (int) (Math.random() * 100 + 1);

          System.out.println("\n" + "Round 2: I am thinking of a number between " + "\n" + "1 and 100, can you guess the number in " + "\n" + "less than 6 attempts?");

          numberOfAttempts = 0;

          int secondRoundGuess = 0;

      do {

          try {

            secondRoundGuess = scan.nextInt();

          if (secondRoundGuess != theNumber) {

            if (secondRoundGuess < theNumber) {

              System.out.println("Sorry " + secondRoundGuess + " is too low, please try again");

            }else{

              System.out.println("Sorry " + secondRoundGuess + " is too high, please try again");  

            }

            numberOfAttempts = numberOfAttempts + 1;
          }

          else {
            System.out.println(secondRoundGuess + " is the correct answer. You got it on " + "\n" + "attempt number " + (numberOfAttempts + 1) + "." + "\n" + "Proceeding to round 3.");

            System.out.println("\n" + "Round 3: I am thinking of a number between " + "\n" + "1 and 1000, can you guess the number in " + "\n" + "less than 10 attempts?");

            theNumber = (int)(Math.random() * 1000 + 1);

            numberOfAttempts = 0;
            int thirdRoundGuess = 0;

            do {

            try {

              thirdRoundGuess = scan.nextInt();

              if (thirdRoundGuess != theNumber) {              

                if (thirdRoundGuess < theNumber) {

                  System.out.println("Sorry " + thirdRoundGuess + " is too low, please try again");                    

                }else {

                  System.out.println("Sorry " + thirdRoundGuess + " is too high, please try again");

                }

                numberOfAttempts = numberOfAttempts + 1;

              }else{
                System.out.println(thirdRoundGuess + " is the correct answer.");

                System.out.println("You won the game, you are a badass." + "\n" + "I made this game and I cant even beat " + "\n" + "level 3.");

                System.out.println("I am a newbie at programming and it's my " + "\n" + "fourth month on this java journey ." + "\n" + "I am looking for fellow newbies  who are willing " + "\n" + "to learn and grow together (or advanced programmers " + "\n" + "who will not get annoyed by teaching newbies)."  + "\n" + "If you get to this point in the game " + "\n" + "and see this message, hit me up via direct " + "\n" + "messaging, lets create a group where we can learn " + "\n" + "and grow together.");

              }

            }catch(Exception e) {
              System.out.println("Please enter a number.");

              thirdRoundGuess = scan.nextInt();
            }

            }while(numberOfAttempts != 10);
            System.out.println("Game over, you were so close. Try again");

          }

          }catch(Exception e) {
            System.out.println("Please enter a number.");

            secondRoundGuess = scan.nextInt();

          }

      }while(numberOfAttempts != 6);

      System.out.println("Game Over. Try again?");

        }

      }catch(Exception e) {
        System.out.println("Please enter a number.");

        guess = scan.nextInt();
      }

    }while (numberOfAttempts != 3);

    System.out.println("Game Over. Try again?");

    scan.close();
  }
}

标签: java

解决方案


这是您在运行代码时获得的异常堆栈跟踪:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at HighLow.main(HighLow.java:137)


当您在用户第一次输入非数字值时处理异常时,将在下面处理异常:

catch(Exception e) {
    System.out.println("Please enter a number.");

    guess = scan.nextInt();
}

当它达到guess = scan.nextInt();时,之前的非数字值仍然存在于扫描仪中。当它有一个非数字值时,当它试图从扫描仪中取出一个整数时,它会抛出另一个未处理的InputMismatchException内部。catch


推荐阅读