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
}
}