簡體   English   中英

兩次運行方法

[英]Running Through Method Twice

我正在為一個班級項目創建一個“猜謎游戲”。 這個想法是,用戶將嘗試猜測一個數字,GUI 將顯示他們剩余的嘗試次數。 MyCustomTooHighException 和 MyCustomTooLowException 都擴展了 Exception。 所以,我的問題是,每當我運行我的代碼時,它似乎都會通過我的 GuessCheckNumber 方法兩次,我不太確定為什么。

這是我的 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;
        }


 }

}

這是 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)));
        }


    }



  }

}

guess.checkGuess(playsguess);

沒有任何作用,因為在下一行中再次調用該方法:

aField.setText(String.valueOf(guess.checkGuess(playsguess)));

只需刪除第一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM