使用抛出语句实现 try-catch 块



在我必须做的猜谜游戏中,我需要包含一个带有两个捕获子句的 try-catch 块(一个用于我的两个自定义异常的多捕获块:BadGuessExceptionTooManyGuessesException,一个块用于NumberFormatException(。

我尝试在我的程序中创建条件来抛出我的自定义异常,因为我不知道它们如何正常工作背后的逻辑。我遇到编译错误,并希望帮助重新编写我的程序,以便它正确实现 try-catch-catch 块。

我的自定义异常类:

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!");
}
}

我的程序出现编译错误:

import java.util.Random;
import java.util.*;
public class GuessingGame throws NumberFormatException
{
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;
//Initialize incrementor for guessing turns
int turn = 1;
//Create variable for user input (guess)
int guess;
try
{
while(guess != n)
{
//Exception handling for more than five turns
if(turn > 5)
throw new TooManyGuessesException();
//Prompt user to enter their guess
System.out.println("Guess a number between 1 and 10 inclusive.");
System.out.println("Hint: the answer is " + n);
//Receive user input (their guess)
guess = keyboard.nextInt();
//Increment turn variable
turn++;
if(guess < 1 || guess > 10)
throw new BadGuessException();
else if(guess == n)
System.out.println("YOU WIN!nIt took you " + turn + " attempts.");
}
}
catch(BadGuessException e | TooManyGuessesException e)
{
e.getMessage();
}
catch(NumberFormatException e)
{
System.out.println("Sorry, you entered an invalid number format.");
}
}
}

在猜谜游戏类中进行更改 在多次尝试块中删除BadGuessException 之后的 e。 并用 0 初始化猜测并从您的类声明中删除数字格式异常;

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;
//Initialize incrementor for guessing turns
int turn = 1;
//Create variable for user input (guess)
int guess = 0 ;
try
{
while(guess != n)
{
//Exception handling for more than five turns
if(turn > 5)
throw new TooManyGuessesException();
//Prompt user to enter their guess
System.out.println("Guess a number between 1 and 10 inclusive.");
System.out.println("Hint: the answer is " + n);
//Receive user input (their guess)
guess = keyboard.nextInt();
//Increment turn variable
turn++;
if(guess < 1 || guess > 10)
throw new BadGuessException();
else if(guess == n)
System.out.println("YOU WIN!nIt took you " + turn + " attempts.");
}
}
catch(BadGuessException | TooManyGuessesException e)
{
e.getMessage();
}
catch(NumberFormatException e)
{
System.out.println("Sorry, you entered an invalid number format.");
}
}
}

最新更新