1

I am creating a rock paper scissors class for my intro to java homework. I am pretty much done but the option to pick a choice is repeating twice and I am getting the wrong output. Im fairly new to java and the methods part me has a bit confused. Thanks for any help! Here is my code

  package rockpaperscissor;

 //imports necessary classes for program
 import java.util.Random; 
 import java.util.Scanner;
 public class RockPaperScissor
 {
/**
 * 
 * @param args 
 */

//main method that runs a loop for how many times the user wants to play
public static void main(String[] args) 
{

    char letter; // choice of playing again
    Scanner keyboard = new Scanner(System.in); // creating an instance of the scanner class
    String computerHand; // string variable for computer choice
    String userHand; // string variable for user choice

     do // do while loop to determine how many times these messages will be displayed
    {

        computerHand = computerHand();
        userHand = userHand();
        String winner = winner(userHand,computerHand);
        System.out.println(winner);
        System.out.println("You chose " + userHand + ".");
        System.out.println("The computer was " + computerHand + ".");


        System.out.println("Would you like to play again");
        String answer = keyboard.nextLine();
        letter = answer.charAt(0);
    }
    //Condition for the do-while loop
    while (letter != 'N' && letter != 'n'); //condition for while loop
}
/**
 * 
 * @return 
 */
public static String userHand() //method for users choice in the game, does not have parameters
{
    //prints message to user giving them choices
    System.out.println("Lets play rock paper scissors. Pick: \n 1. rock \n 2. paper \n 3. scissors.");
    int userChoice; // user choice variable in this method
    Scanner keyboard = new Scanner(System.in); // creates instance of scanner class
    userChoice = keyboard.nextInt(); //reads user input
    return masterChoice(userChoice); //returns user choice to master choice
}
/**
 * 
 * @return 
 */
public static String computerHand() //method for computer randomly generated choice
{
    Random randomNum = new Random();
    int computerGenerator = randomNum.nextInt(3) +1;
    return masterChoice(computerGenerator);
} 
/**
 * 
 * @param num
 * @return 
 */
public static  String masterChoice(int num) //method recieving both computer hand and user hand
{
    //if statment to math numeric choice with string output
    String choice = null;
  if (num == 1){
      choice = "rock";
  }
  else if(num == 2){
      choice = "paper";
  }
  else if(num == 3){
      choice = "scissors";
  }
    return choice;
}

/**
 * 
 * @param computerChoice
 * @param userChoice
 * @return 
 */
public static String winner(String computerChoice, String userChoice) //method to determine winner
{
        computerChoice = computerHand(); //places computerChoice variable in computerhand
        userChoice = userHand(); //does same for user choice
        String winner=null;

        //multiple if statements determining winner
        if (userChoice.equalsIgnoreCase("rock") && computerChoice.equalsIgnoreCase("scissors")){
            winner = "Rock beats scissors, you win!";
        }
        else if (userChoice.equalsIgnoreCase("rock") && computerChoice.equalsIgnoreCase("paper")){
            winner = "Rock loses to paper, you lose";
        }
        else if (userChoice.equalsIgnoreCase("rock") && computerChoice.equalsIgnoreCase("rock")){
            winner = "Its a tie";
        }
        else if (userChoice.equalsIgnoreCase("paper") && computerChoice.equalsIgnoreCase("scissors")){
            winner = "Paper loses to scissors, you lose";
        }
        else if (userChoice.equalsIgnoreCase("paper") && computerChoice.equalsIgnoreCase("rock")){
            winner = "Paper beats rock, you win!";       
        }
        else if (userChoice.equalsIgnoreCase("paper") && computerChoice.equalsIgnoreCase("paper")){
            winner = "Its a tie";
        }
        else if (userChoice.equalsIgnoreCase("scissors") && computerChoice.equalsIgnoreCase("scissors")){
            winner ="Its a tie";
        }
        else if (userChoice.equalsIgnoreCase("scissors") && computerChoice.equalsIgnoreCase("paper")){
            winner = "Scissors beats paper, you win!";
        }
        else if (userChoice.equalsIgnoreCase("scissors") && computerChoice.equalsIgnoreCase("rock")){
            winner = "Scissors loses to rock";
        }
        return winner; //returns winner to main

}

}
javaGuy123456
  • 41
  • 1
  • 1
  • 9
  • 1
    Menu is printed twice because both computerHand and userHand are being called twice, from the main loop, and from the winner method. Remove these two calls from the "winner" method better! – Juan Dec 07 '15 at 21:19

2 Answers2

3

The problem is that in your main method, you are calling computerHand() and userHand(), but inside your winner() method, you are again calling computerHand() and userHand().

Your winner() method does not need to call these methods, as the user and computer's choices are being passed in.

public static String winner(String computerChoice, String userChoice) //method to determine winner
{
    // computerChoice = computerHand(); //places computerChoice variable in computerhand
    // userChoice = userHand(); //does same for user choice
    String winner=null;
    ...

Edit - the reason the results are wrong is that your winner() method takes computerChoice first then userChoice, but when you call the method, you are passing userHand first, then computerHand.

Change the call to:

String winner = winner(computerHand, userHand);
Jason
  • 11,744
  • 3
  • 42
  • 46
  • Ok that fixed that. Thank you! Also do you happen to know why my winner method is not displaying the right winner? – javaGuy123456 Dec 07 '15 at 21:23
  • Edited to include answer. Also, it is generally bad form to have variables with the same name as your methods. Typically (but not always) your methods should start with a verb - such as `getUserHand()` or `calculateWinner()`. – Jason Dec 07 '15 at 21:28
0

When you select the user input using keyboard.nextInt() it returns only the next integer on the line. When the code continues and gets to String answer = keyboard.nextLine() it gets the rest of the line (which is basically nothing and then the end of line character(s)) and continues down to the While condition. The answer is not "N" and it is not "n" so the loop runs again.

See this StackExchange question: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

Community
  • 1
  • 1
Tophandour
  • 297
  • 5
  • 15