This document is a programming guide for creating a Rock, Paper, Scissors game in Java. It covers key concepts such as conditionals, control flow, and methods, providing sample code and output examples. Ideal for students and educators, this resource is part of a coding curriculum.
/ 3

4.7.11 Rock, Paper, Scissors!
CodeHS — Java • Conditionals & Control Flow • Methods
Assignment Description
In this exercise, you build an interactive Rock, Paper, Scissors game in Java. The program asks the user
to choose rock, paper, or scissors, then the computer randomly selects one of the three options. The
program determines and prints the winner. The game continues in a loop until the user presses Enter
without typing anything.
You must implement a method called String getWinner(String user, String computer) that
takes both choices and returns a string indicating the result. The computer's choice is generated using
Randomizer.nextInt(1, 3), mapping 1 to rock, 2 to paper, and 3 to scissors.
Sample Output
Enter your choice (rock, paper, or scissors): rock
User: rock
Computer: paper
Computer wins!
Enter your choice (rock, paper, or scissors): paper
User: paper
Computer: scissors
Computer wins!
Enter your choice (rock, paper, or scissors): scissors
User: scissors
Computer: paper
User wins!
Enter your choice (rock, paper, or scissors): rock
User: rock
Computer: rock
Tie
Enter your choice (rock, paper, or scissors):
Thanks for playing!
Source Code
RockPaperScissors.java

public class RockPaperScissors extends ConsoleProgram
{
private static final String USER_PLAYER = "User wins!";
private static final String COMPUTER_PLAYER = "Computer wins!";
private static final String TIE = "Tie";
private static final String ROCK = "rock";
private static final String PAPER = "paper";
private static final String SCISSORS = "scissors";
private String getWinner(String user, String computer)
{
if (user.equals(computer))
return TIE;
else if (user.equals(ROCK) && computer.equals(PAPER))
return COMPUTER_PLAYER;
else if (user.equals(PAPER) && computer.equals(SCISSORS))
return COMPUTER_PLAYER;
else if (user.equals(SCISSORS) && computer.equals(ROCK))
return COMPUTER_PLAYER;
else
return USER_PLAYER;
}
public void run()
{
String choice = readLine("Enter your choice (rock, paper, or scissors): ");
while (!choice.equals(""))
{
System.out.println("User: " + choice);
int compInt = Randomizer.nextInt(1, 3);
String comp = "";
if (compInt == 1)
comp = "rock";
else if (compInt == 2)
comp = "paper";
else if (compInt == 3)
comp = "scissors";
System.out.println("Computer: " + comp);
System.out.println(getWinner(choice, comp));
System.out.println();
choice = readLine("Enter your choice (rock, paper, or scissors): ");
}
System.out.println("Thanks for playing!");
}
}

Game Logic Reference
User Computer Result
rock scissors User wins!
rock paper Computer wins!
rock rock Tie
paper rock User wins!
paper scissors Computer wins!
paper paper Tie
scissors paper User wins!
scissors rock Computer wins!
scissors scissors Tie
Key Concepts Demonstrated
Concept How It's Used
Constants static final String constants store fixed values for choices and
outcomes, making the code cleaner and easier to maintain.
Methods The getWinner() method encapsulates the game logic separately from input
handling, demonstrating modular programming.
If/Else If Chains Nested conditionals evaluate all possible game outcomes: user win, computer
win, or tie.
While Loop The game runs repeatedly until the user enters an empty string, demonstrating
loop-based control flow.
Randomizer Randomizer.nextInt(1, 3) generates the computer's random choice,
which is then mapped to a string using if/else.
String Comparison Uses .equals() for comparing strings rather than ==, which is essential in
Java for comparing string content.
CodeHS 4.7.11 Rock, Paper, Scissors! — Java Conditionals & Methods Exercise
Loading document...
/ 3
Upload to Download
Every 3 documents you upload earns 1 download credit.
You have uploaded 0 documents. Upload 3 more to earn a download.
Upload Documents
End of Document
228
Report this document
Why are you reporting this content?
Embed document
* The code will be updated based on your changes.
Upload to Print
Every 3 documents you upload earns 1 print/download credit.
You have uploaded 0 documents. Upload 3 more to earn a credit.
Upload Documents