lundi 9 octobre 2017

How to dynamically select which variable to return in if else statement

Since you can't select more than one value to return in Java, I was wondering how I could set my method to return the correct value based on the if else statement. How I have it now with a return in each if statement probably isn't correct but I'm not sure of an alternative. I think the code is just choosing the first return statement and closing the program. What would be an alternative? Thanks

public int rockGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Rock vs Rock equals a tie! \n");
               return result3++;
           }
           else if (compChoice == 1)
           {
               txaResults.append("Paper Covers Rock (Computer Wins)! \n");
               return result2++;
           }
           else if (compChoice == 2)
           {    
               txaResults.append("Rock smashes scissors (You Win) \n");
               return result1++;
           } 
           return 0;
       }

here's my code. It's an actionListener so when I hit a button I want the respective method with it's return value to run. Eventually, I'll have it output to a JTextField if all goes to plan. Right now when I hit a button, I get the same outcome, the first condition, for each method like rockGame(), paperGame(), etc.

 class RPSAppenderAction implements ActionListener
   { 
        Random random = new Random();
        int compChoice = random.nextInt(3);
        int result1 = 0; //user wins
        int result2 = 0; //computer wins
        int result3 = 0; // tie

        public int rockGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Rock vs Rock equals a tie! \n");
               return result3++;
           }
           else if (compChoice == 1)
           {
               txaResults.append("Paper Covers Rock (Computer Wins)! \n");
               return result2++;
           }
           else if (compChoice == 2)
           {    
               txaResults.append("Rock smashes scissors (You Win) \n");
               return result1++;
           } 
           return 0;
       }

       public int paperGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Paper covers rock (You win)! \n"); 
                return result1++;
           }
           else if (compChoice == 1)
           {
               txaResults.append("Paper vs Paper equals a tie \n");
               return result3++;
           }
           else if (compChoice == 2)
           {    
               txaResults.append("Scissors cuts Paper(You Lose) \n"); 
               return result2++;
           } 
           return 0;
       }

       public int scissorsGame()
       {
           if (compChoice == 0)
           {
               txaResults.append("Rock smashes scissors (You Lose)! \n"); 
               return result2++;
           }
           else if (compChoice == 1)
           {
               txaResults.append("Scissors cut paper (You Win)! \n");;
               return result1++;
           }
           else if (compChoice == 2)
           {    
               txaResults.append("The game is a tie! \n"); 
               return result3++;
           } 
           return 0;
       }


       @Override
       public void actionPerformed ( ActionEvent click )
       {
           Object source = click.getSource();
           Random random = new Random();
           int compChoice = random.nextInt(3);
           int count1 = 0; //tieCount
           int count2 = 0; //userCount
           int count3 = 0; //compCount


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

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

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

       } 
    }

Aucun commentaire:

Enregistrer un commentaire