【问题标题】:Java- Debugging if-else statementJava-调试 if-else 语句
【发布时间】:2019-11-27 14:48:44
【问题描述】:

我已经研究了好几个小时的代码,但找不到任何适合我的代码的解决方案 当我点击我的 Water 、 Fire 或 Nature 按钮时,游戏 GUI 不应该再显示了,因为每个按钮都会添加 1 存储到我的游戏播放中,最多只能存储 5 次,我真的想不通这个出来请帮帮我!真的很感激。

public class Game {
   private JPanel Game;
   private JButton Water;
   private JButton Nature;
   private JButton Fire;
   private int myChoice;
   private int computerChoice;
        int gamePlayed = 0; //store the times of game played

 public Game() {
    JFrame frame = new JFrame("The Elements");
    frame.setContentPane(this.Game);
    frame.setMinimumSize(new Dimension(500, 500));
    frame.pack();
    frame.setVisible(true);


      if (gamePlayed <= 5) {  //if the times of game played is less than 5 times , execute the following code

        Water.addActionListener(new ActionListener() {
            @Override
            //Water counter Fire , Nature counter water
            public void actionPerformed(ActionEvent e) {
                int select;
                select = JOptionPane.showConfirmDialog(null,
                        "You're using the power of water", "Water",
                        JOptionPane.YES_NO_OPTION);

                if (select == JOptionPane.YES_OPTION) {
                    myChoice = 0;
                    computerChoice = computerPlays();
                    conditionsDisplayResults(myChoice, computerChoice);
                    gamePlayed+=1;//add 1 time of played game when the yes option clicked

                }

            }
        });
        Fire.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int select = JOptionPane.showConfirmDialog(null,
                        "You're using the power of fire", "Fire",
                        JOptionPane.YES_NO_OPTION);

                if (select == JOptionPane.YES_OPTION) {
                    myChoice = 1;
                    computerChoice = computerPlays();

                    conditionsDisplayResults(myChoice, computerChoice);
                    gamePlayed+=1;//add 1 time of played game when the yes option clicked
                }
            }
        });
        Nature.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int select = JOptionPane.showConfirmDialog(null,
                        "You're using the power of nature", "Nature",
                        JOptionPane.YES_NO_OPTION);

                if (select == JOptionPane.YES_OPTION) {
                    myChoice = 2;
                    computerChoice = computerPlays();
                    conditionsDisplayResults(myChoice, computerChoice);
                    gamePlayed+=1;//add 1 time of played game when the yes option clicked
                }

            }
        });


  else{ //else which when the times of game played is more than 5 times , execute the following code

        GameResult();
        MainMenu mainMenu = new MainMenu();

    }
}

【问题讨论】:

  • 为什么投反对票?怀疑是“请调试我的代码?” @ShangHuang,这个问题真的不清楚。有什么错误?你熟悉debugging吗?
  • 对不起,我刚刚意识到我提出了一个错误的问题标题。谢谢你给我建议!我会记住这一点! :D
  • 我试图编辑标题,现在可以吗?
  • Debugging! Debugging 会救你一命!

标签: java swing user-interface


【解决方案1】:

您的条件if (gamePlayed &lt;= 5) 被测试一次:在程序开始时。 当您的一个按钮将 +1 添加到 gamePlayed 时,您就不会在初始状态下再次访问。

有很多方法可以解决这个问题,最简单的方法是在按钮单击事件中退出程序,例如:

public class Game
{
    private JPanel  Game;
    private JButton Water;
    private JButton Nature;
    private JButton Fire;
    private int         myChoice;
    private int         computerChoice;
    int                         gamePlayed  = 0;    //store the times of game played

    public Game()
    {
        JFrame frame = new JFrame( "The Elements" );
        frame.setContentPane( this.Game );
        frame.setMinimumSize( new Dimension( 500, 500 ) );
        frame.pack();
        frame.setVisible( true );

        Water.addActionListener( new ActionListener()
        {
            @Override
            //Water counter Fire , Nature counter water
            public void actionPerformed( ActionEvent e )
            {
                int select;
                select = JOptionPane.showConfirmDialog( null, "You're using the power of water", "Water", JOptionPane.YES_NO_OPTION );

                if ( select == JOptionPane.YES_OPTION )
                {
                    myChoice = 0;
                    computerChoice = computerPlays();
                    conditionsDisplayResults( myChoice, computerChoice );
                    gamePlayed += 1;//add 1 time of played game when the yes option clicked

                }

                if ( this.gamePlayed > 5 )
                {
                    exit();
                }
            }
        } );
        Fire.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                int select = JOptionPane.showConfirmDialog( null, "You're using the power of fire", "Fire", JOptionPane.YES_NO_OPTION );

                if ( select == JOptionPane.YES_OPTION )
                {
                    myChoice = 1;
                    computerChoice = computerPlays();

                    conditionsDisplayResults( myChoice, computerChoice );
                    gamePlayed += 1;//add 1 time of played game when the yes option clicked
                }

                if ( this.gamePlayed > 5 )
                {
                    exit();
                }
            }
        } );
        Nature.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                int select = JOptionPane.showConfirmDialog( null, "You're using the power of nature", "Nature", JOptionPane.YES_NO_OPTION );

                if ( select == JOptionPane.YES_OPTION )
                {
                    myChoice = 2;
                    computerChoice = computerPlays();
                    conditionsDisplayResults( myChoice, computerChoice );
                    gamePlayed += 1;//add 1 time of played game when the yes option clicked
                }

                if ( this.gamePlayed > 5 )
                {
                    exit();
                }
            }
        } );
    }

    void exit()
    {
        // exit code
    }
}

【讨论】:

  • 感谢您帮助我!它现在让我头脑清醒!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2015-03-13
  • 2023-03-28
  • 2019-04-09
  • 2017-02-17
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多