
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