方法在调用时运行一次,并在再次调用时提供相同的输出

时间:2017-10-10 02:30:14

标签: java methods jbutton jtextfield

class RPSAppenderAction implements ActionListener
   { 
        Random random = new Random();
        int compChoice = random.nextInt(3);
        int[] outcome = new int[3];

        public void rockGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Rock vs Rock equals a tie! \n");
               outcome[2]++;
               Tie.setText("Total " + outcome[2]);
           }
           else if (compChoice == 1)
           {
               txaResults.append("Paper Covers Rock (Computer Wins)! \n");
               outcome[1]++;
               CompWins.setText("Total " + outcome[1]);
           }
           else if (compChoice == 2)
           {    
               txaResults.append("Rock smashes scissors (You Win) \n");
               outcome[0]++;
               UserWins.setText("Total " + outcome[0]);
           } 
       }

       public void paperGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Paper covers rock (You win)! \n"); 
               outcome[0]++;
               UserWins.setText("Total " + outcome[0]);
           }
           else if (compChoice == 1)
           {
               txaResults.append("Paper vs Paper equals a tie \n");
               outcome[2]++;
               Tie.setText("Total " + outcome[2]);
           }
           else if (compChoice == 2)
           {    
               txaResults.append("Scissors cuts Paper(You Lose) \n"); 
               outcome[1]++;
               CompWins.setText("Total " + outcome[1]);
           } 
       }

       public void scissorsGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Rock smashes scissors (You Lose)! \n"); 
               outcome[1]++;
               CompWins.setText("Total " + outcome[1]);
           }
           else if (compChoice == 1)
           {
               txaResults.append("Scissors cut paper (You Win)! \n");;
               outcome[0]++;
               UserWins.setText("Total " + outcome[0]);
           }
           else if (compChoice == 2)
           {    
               txaResults.append("The game is a tie! \n"); 
               outcome[2]++;
               Tie.setText("Total " + outcome[2]);
           } 
       }

       @Override
       public void actionPerformed ( ActionEvent click )
       {
           Object source = click.getSource();

           if (source == btnRock)
           {
               rockGame();
           }

           if (source == btnPaper)
           {
               paperGame();
           }

           if (source == btnScissors)
           {
               scissorsGame();
           }

       } 
    }

这里我有一个actionListener,所以每当有人点击摇滚,纸张或剪刀的相应JButton时,就会调用rockGame(),paperGame()或scissorGame()方法。但是,第一次当我选择说摇滚时,程序将正常运行,但如果我再次击中摇滚,那么每次输出都是相同的(相同的txaResult附加到JScrollPane并且JTextField中的结果[]计数相同) 。它从第一次调用方法时获取相同的结果,而不是为每次运行调用该方法的不同实例。当有人点击JButton而不是仅仅使用第一个结果时,有没有办法让我再次调用相应的方法?谢谢。我已经讨论过这个问题一段时间了,什么都想不到

0 个答案:

没有答案