Skip to content

Having to resubmit user choice over and over

Hey guys having some trouble with a multi part question. I’m trying to get a game of rock paper scissors done, when i try to test the code using the playRound method I have to input my choice over and over again and then it just prints out draw, you are the winner and the computer is the winner over and over, can anyone tell me where im going wrong ?

import java.util.Scanner;
import java.util.Random;
/**
 * A class that will play a game of rock paper scissors.
 *
 * @author (your name)
 * @version (a version number or a date)
 */

public class RockPaperScissors
{
    private Scanner reader;
    private int yourScore;
    private int computerScore;
    private Random ran = new Random();

    public RockPaperScissors()
    {
        reader = new Scanner(System.in);
        yourScore = 0;
        computerScore=0;
        Random ran = new Random(); 
    }

    public void printPrompt()
    {
        System.out.println("Enter your choice, paper, rock or scissors >"); 
        String userChoice = userChoice();
        System.out.println();
        System.out.println();
        System.out.println("Enter your choice, paper, rock or scissors  >"+ userChoice);
    }

    public final String userChoice()
    {
        String userChoice= reader.next();
        return userChoice;
    }

    public String computerChoice()
    {
        String compMove = ("");
        int cpuChoice = ran.nextInt(3);
        switch(cpuChoice)
        {
            case 0:
            {
                compMove = ("rock");
                break;
            }
            case 1:
            {
                compMove = ("paper");
                break;
            }
            case 2:
            {
                compMove = ("scissors");
                break;
            }

        }
        return (compMove);
    }

    public String findWinner(String yourChoice, String computerChoice) 
    {
        yourChoice = userChoice();
        computerChoice = computerChoice();
        String Winner= null;

        if (yourChoice.equals(computerChoice))
        {
            Winner = ("draw");
        }
        if (yourChoice.equals("rock")) 
        {
            if (computerChoice.equals("paper")) 
            {
                computerScore++;
                Winner = ("computer");

            }
            else if (computerChoice == "scissors")
            {
                yourScore++;
                Winner = ("you");

            }
        }
        if (yourChoice.equals("paper"))
        {
            if (computerChoice.equals("scissors"))
            {
                computerScore++;
                Winner = ("computer");

            }
            else if (computerChoice.equals("rock")) 
            {
                yourScore++;
                Winner = ("you");

            }
        }
        if (yourChoice.equals("scissors"))
        {
            if (computerChoice.equals("rock"))
            {
                computerScore ++;
                Winner = ("computer");

            }
            else if (computerChoice.equals("paper"))
            {
                yourScore++;
                Winner = ("you");

            }
        }
        if (!yourChoice.equals("rock||paper||scissors")) 
        {
            computerScore++;
            Winner = ("computer");

        }

        return Winner;
    }

    public void playRound()
    {
        printPrompt();
        String computerChoice=computerChoice();
        String userChoice=userChoice();
        System.out.println("You have chosen " + userChoice + " and the computer has chosen " + computerChoice);
        String findWinner = findWinner(computerChoice,userChoice);
        
        {
            if (findWinner.equals("draw"));
            System.out.println("This game is a draw");
            if (findWinner.equals("you"));
            System.out.println("You are the winner");
            if (findWinner.equals("computer"));
            System.out.println("The computer is the winner");
        }
        System.out.println("You have " + yourScore + "and the computer has "+ computerScore);
    }
}

Answer

The semicolon at the end of your if statements means that the if statement is COMPLETE. The lines immediately following your if statements are then stand-alone, they do not “belong” to the if statements…which is why they always run.

Change:

if (findWinner.equals("draw"));
System.out.println("This game is a draw");

To:

if (findWinner.equals("draw"))
    System.out.println("This game is a draw");

Or even better:

if (findWinner.equals("draw")) {
    System.out.println("This game is a draw");
}

Any advice on how i can get my initial choice to be saved so I don’t have to keep typing it in ?

In findWinner(), you’re calling userChoice() and computerChoirce() AGAIN. At that point, the values are already in the parameters that were passed in, so you don’t need those. Comment them out or remove them:

public String findWinner(String yourChoice, String computerChoice) 
{
    // You don't need these two lines:
    //yourChoice = userChoice();
    //computerChoice = computerChoice(); 

    // ... other existing code ...
}

Then, in playRound(), you are calling both printPrompt() and userChoice(), which each have their own input mechanism:

printPrompt();
String computerChoice=computerChoice();
String userChoice=userChoice();
System.out.println("You have chosen " + userChoice + " and the computer has chosen " + computerChoice);

I’d get rid of printPrompt() and just do:

// printPrompt();
String computerChoice=computerChoice();
System.out.print("Enter your choice, paper, rock or scissors >"); 
String userChoice=userChoice();
System.out.println("You have chosen " + userChoice + " and the computer has chosen " + computerChoice);

You have some broken logic in findWinner(). This does not do what you think it does:

if (!yourChoice.equals("rock||paper||scissors")) 
{
    computerScore++;
    Winner = ("computer");
}

I think what you were trying to do is make the computer win if the user did not type in “rock”, “paper”, or “scissors”? If so, change your if statements to else if statements and add a final else block:

public String findWinner(String yourChoice, String computerChoice) 
{
    String Winner = "";

    if (yourChoice.equals(computerChoice))
    {
        Winner = "draw";
    }
    else if (yourChoice.equals("rock")) 
    {
        if (computerChoice.equals("paper")) 
        {
            computerScore++;
            Winner = "computer";

        }
        else if (computerChoice.equals("scissors"))
        {
            yourScore++;
            Winner = "you";

        }
    }
    else if (yourChoice.equals("paper"))
    {
        if (computerChoice.equals("scissors"))
        {
            computerScore++;
            Winner = "computer";

        }
        else if (computerChoice.equals("rock")) 
        {
            yourScore++;
            Winner = "you";

        }
    }
    else if (yourChoice.equals("scissors"))
    {
        if (computerChoice.equals("rock"))
        {
            computerScore ++;
            Winner = "computer";

        }
        else if (computerChoice.equals("paper"))
        {
            yourScore++;
            Winner = "you";

        }
    }
    else // user did not type "rock", "paper", or "scissors"!
    {
        computerScore++;
        Winner = ("computer");

    }

    return Winner;
}

Lastly, MAJOR bug, you SWAPPED the order of your parameters when you called findWinner().

Change:

String findWinner = findWinner(computerChoice, userChoice);

To:

String findWinner = findWinner(userChoice, computerChoice);

Hope that helps!