I am creating a "Guessing Game" for a class project. The idea is that the user will try to guess a number and the GUI will show how many attempts they have left. MyCustomTooHighException and MyCustomTooLowException both extend Exception. So, my problem is that whenever I run my code it seems to go through my GuessCheckNumber method twice and I'm not too sure why.
This is my GuessTheNumberMethod:
public class GuessTheNumber extends JFrame{
public int random;
public int attempts;
//public int guess;
private String title;
public GuessTheNumber(){
attempts = 0;
}
public int checkGuess(int guess) {
Random rand = new Random();
random = rand.nextInt(100) +1;
MyCustomException tj = new MyCustomException();
try {
System.out.println(guess);
if (guess > random) {
throw new MyCustomTooHighException(guess);
} else if (guess < random) {
throw new MyCustomTooLowException(guess);
}
} catch (MyCustomTooHighException e) {
attempts++;
JOptionPane.showMessageDialog(null, "The value is smaller", "Alert", JOptionPane.ERROR_MESSAGE);
} catch (MyCustomTooLowException e) {
attempts++;
JOptionPane.showMessageDialog(null, "The value is bigger", "Alert", JOptionPane.ERROR_MESSAGE);
} finally {
if(guess == random)
{
JOptionPane.showMessageDialog(null, "You Won!", "Alert", JOptionPane.ERROR_MESSAGE);
}else if(guess != random && attempts<10)
JOptionPane.showMessageDialog(null, "Guess Again\nYou have "+ (10-attempts) +" attempts left." , "Alert", JOptionPane.ERROR_MESSAGE);
if(attempts ==10){
JOptionPane.showMessageDialog(null, "Game Over!", "Alert", JOptionPane.ERROR_MESSAGE);
}
return 10-attempts;
}
}
}
And this is the JPanel Class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
public class GuessGamePanel extends JPanel {
private GuessTheNumber guess;
JButton submitButton;
JTextField gField;
JLabel aField;
public GuessGamePanel(){
setLayout(new GridLayout(5,2));
setBackground(Color.lightGray);
guess = new GuessTheNumber();
add(new JLabel("Pick a number between 1 -10."));
add(new JLabel("Guess:"));
gField = new JTextField("0");
add(gField);
submitButton = new JButton("Submit");
add(submitButton);
submitButton.addActionListener(new ButtonListener());
aField = new JLabel();
add(new JLabel("Attempts Left:"));
add(aField);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int playsguess;
if(submitButton ==e.getSource()){
playsguess=Integer.parseInt(gField.getText());
guess.checkGuess(playsguess);
aField.setText(String.valueOf(guess.checkGuess(playsguess)));
}
}
}
}
The line
guess.checkGuess(playsguess);
serves no purpose as the method is called again in the next line:
aField.setText(String.valueOf(guess.checkGuess(playsguess)));
Just remove the first line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.