在我必须做的猜谜游戏中,我需要包含一个带有两个catch子句的try-catch块(一个针对我的两个自定义异常:BadGuessException
和TooManyGuessesException
的多重catch块,以及一个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.");
}
}
}
答案 0 :(得分:1)
进行GuessingGame类中的更改在多个try块中,在BadGuessException之后删除 e 。并用0初始化猜测,并从类声明中删除NumberformatException;
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.");
}
}
}