Java 根据计数器变量关闭游戏窗口

Java 根据计数器变量关闭游戏窗口,java,Java,我要做的是创建一个int,每当我在确认对话框中单击yes时,int将增加1,当存储到该int中的数字达到5时,整个程序应该停止执行 代码不起作用,并且没有任何事情发生。无论我在确认对话框中单击“是”按钮多少次,我都希望当我单击“是”按钮5次时,代码停止 我是一个仍在努力学习java的初学者,我不知道我在代码上犯了什么错误,因为它们在逻辑上似乎是正确的,如果有人能帮我一把,我真的很感激!非常感谢 这是主要的游戏 public class Game { private JPan

我要做的是创建一个int,每当我在确认对话框中单击yes时,int将增加1,当存储到该int中的数字达到5时,整个程序应该停止执行

代码不起作用,并且没有任何事情发生。无论我在确认对话框中单击“是”按钮多少次,我都希望当我单击“是”按钮5次时,代码停止

我是一个仍在努力学习java的初学者,我不知道我在代码上犯了什么错误,因为它们在逻辑上似乎是正确的,如果有人能帮我一把,我真的很感激!非常感谢

这是主要的游戏

   public class Game {
        private JPanel Game;
        private JButton Water;
    private JButton Nature;
    private JButton Fire;
    private JLabel countDownLabel;
    private JLabel computerScore;
    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);
如果玩游戏的次数少于5次,请执行以下代码

               if (gamePlayed <= 5) {
                   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 = 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
                           }


                       }
                   } );
               }

GameResult()所做的是System.exit(0),它退出程序

据我所知,您希望
gamePlayed
变量数到五,然后程序停止


问题在于,您只测试了
if(gamePlayed问题实际上是缺少停止条件,并且没有正确添加侦听器。用于检查点击次数的代码应该位于添加的匿名ActionListener类中,并且只添加了一次。类似于此:

Water.addActionListener( new ActionListener()
                       {
                           @Override
                           //Water counter Fire , Nature counter water
                           public void actionPerformed( ActionEvent e )
                           {
                               //check if game is over
                               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 = gamePlayed + 1;//add 1 time of played game when the yes option clicked

                               }
                           }
                       } );

您应该检查游戏是否在每个按钮的侦听器中结束,因为代码不是按顺序运行的,而是由事件触发的,在这种情况下,用户单击。

您说它“错了,”但是您没有指出是什么原因造成了错误。您的代码应该如何运行?它实际上是如何运行的?请尽可能描述,因为如果我们必须进行假设或猜测,我们无法给您答案。感谢您的建议,现在可以吗?您没有在编辑中添加任何新信息。只是重新安排了您的代码文本一点。所以不,它不清楚。我也在我的代码上写了一些注释,你能告诉我你不理解代码的哪一部分吗?一般来说,如果你的问题没有足够的解释让我们知道我们在你的代码中寻找什么,并且你的代码比几行长,那么我们就不会阅读代码来试图理解你想做什么。问题的前提是代码没有做你想做的事情,因此你想做的事情可能无法从代码中推断出来;文本中的清晰解释是必要的。
Water.addActionListener( new ActionListener()
                       {
                           @Override
                           //Water counter Fire , Nature counter water
                           public void actionPerformed( ActionEvent e )
                           {
                               //check if game is over
                               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 = gamePlayed + 1;//add 1 time of played game when the yes option clicked

                               }
                           }
                       } );