【问题标题】:How to implement custom exceptions for guessing game如何实现猜谜游戏的自定义异常
【发布时间】:2019-01-20 13:37:41
【问题描述】:

我必须创建一个最多允许 5 个回合的猜谜游戏,并且用户输入必须介于 1 和 10 之间。如果不满足这些条件,则可能会引发两个自定义异常(BadGuessException 和 TooManyGuessesException)。我被困在如何进行异常处理上,因为我不确定如何让程序知道是否抛出和捕获这些自定义异常。

我为自定义异常创建了两个类:

public class BadGuessException extends Exception
{
  /**
   * no-arg constructor
   */
  public BadGuessException()
  {
    super("Sorry, that was an invalid guess!");
  }

  /**
   * parametrized constructor
   * @param message String message passed to super class's constructor
   */
  public BadGuessException(String message)
  {
    super(message);
  }
}

public class TooManyGuessesException extends Exception
{
  /**
   * no-arg constructor
   */
  public TooManyGuessesException()
  {
    super("Sorry, too many guesses!");
  }

  /**
   * parametrized constructor
   * @param guess integer value representing amount of guesses (turns)
   */
  public TooManyGuessesException(int guess)
  {
    super("Sorry, you guessed " + guess + " times!");
  }

}

在下面的代码中,我试图在抛出 TooManyGuessesException 之前允许最多五圈,并且我试图处理小于 1 和大于 10 的数字输入的异常。我是必需的只有一个 try-catch 块(以及 NumberFormatException 的额外 catch 子句)。

import java.util.Random;
import java.util.*;

public class GuessingGame
{
  public static void main(String[] args)
  {
    //Scanner object to receive user input
    Scanner keyboard = new Scanner(System.in);

    //Create Random class object & random variable
    Random rng = new Random();
    int n = rng.nextInt(10 - 1 + 1) + 1;

    //Create incrementor for guessing turns
    int turn = 1;
    //Create variable for user input (guess)
    int guess;


    try
    {
      while(guess != n && turn <= 5)
      System.out.println("Guess a number between 1 and 10 inclusive.");
      System.out.println("Hint: the answer is " + n);
      guess = keyboard.nextInt();
      turn++;
      if(guess == n)
      {
        System.out.println("YOU WIN!\nIt took you " + turn + " attempts.");
      }

    }
    catch(BadGuessException e | TooManyGuessesException e)
    {
      if(guess < 1 || guess > 10)
        e.BadGuessException();
      if(turn > 5)
        e.TooManyGuessesException();
    }
    catch(NumberFormatException e)
    {
      System.out.println("Sorry, you entered an invalid number format.");
    }
  }
}

【问题讨论】:

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


【解决方案1】:

@Jack Flamp 刚才说的完全正确,但是您不想检查猜测是否在 1 和 10 之间,而是要检查用户猜测是否与您的随机数匹配。

if (guess == n) {
    System.out.println("YOU WIN!\nIt took you " + turn + " attempts.");
} else {
    throw new BadGuessException();
}

如果你想在 turn 变量超过 5 时抛出异常,将其从 while 条件中删除,并在 while 范围的开头创建一个 if:

if (turn > 5)
    throw new TooManyGuessesException();

PS : 在你的 while 范围内加上括号,否则你会立即有一个 TooManyGuessesException

PS2 : 我想评论 Jack Flamp 的消息,但由于我的声望没有 50,所以我不能。

【讨论】:

    【解决方案2】:

    先检查输入的数字是否满足条件,然后再与答案进行比较:

    if (guess < 1 || guess > 10) {
        throw new BadGuessException();
    } else if (guess == n) {
        System.out.println("YOU WIN!\nIt took you " + turn + " attempts.");
    }
    

    至于TooManyGuessesException,它永远不会被抛出,因为您将while 循环限制为在一定次数的迭代后结束。从循环条件中删除 turn 并在检查轮数是否过多的 if 语句中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2020-11-22
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多