简体   繁体   English

两次运行方法

[英]Running Through Method Twice

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.这个想法是,用户将尝试猜测一个数字,GUI 将显示他们剩余的尝试次数。 MyCustomTooHighException and MyCustomTooLowException both extend Exception. MyCustomTooHighException 和 MyCustomTooLowException 都扩展了 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.所以,我的问题是,每当我运行我的代码时,它似乎都会通过我的 GuessCheckNumber 方法两次,我不太确定为什么。

This is my GuessTheNumberMethod:这是我的 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:这是 JPanel 类:

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.只需删除第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM