Question
1. Write a java program that allows a user to play 5 rounds of Rock-Paper-Scissors against a computer opponent. The user will be prompted with
1. Write a java program that allows a user to play 5 rounds of “Rock-Paper-Scissors” against a computer opponent.
The user will be prompted with a choice between Rock, Paper and Scissors. A random selection between Rock, Paper and Scissors
is made on behalf of the computer. The result of the round will be printed to the console along with the user’s statistics. When all
rounds are complete, print the user’s win percentage.
A round has only two possible outcomes: a draw, or a win for one player and a loss for the other. A draw happens when both players
choose the same item.
The following shows which items win over other items.
Rock beats Scissors
Paper beats Rock
Scissors beats Paper
Requirements
For each round, print the number of the round. Use the following text:
Round {number}
Prompt the user for their choice of Rock-Paper-Scissors using the following text:
Enter your choice (1 - Rock, 2 - Paper, 3 - Scissors): {user_response}
When the user enters an invalid choice, print the error message “Invalid Choice!”. Prompt the user to enter a new choice. Use the
following text:
Invalid choice! (1, 2 or 3): {user_response}
NOTE: Continue to prompt the user for a choice until they enter a valid integer.
Your program will chose an item for the computer player at random. Print the choice made by the computer to the console:
Computer chose {choice}.
A round is completed when a winner is determined. In the case of a draw, print the following message to the console:
The round is a draw.
Replay the round until there is a winner.
When a winner is determined, print a message indicating which item beat the other item followed by the winner’s name (“Player” or
“Computer”). Use the following text and format:
{winner_choice} beats {loser_choice}, {player} wins!
At the end of each round print a message indicating how many rounds the user has won. Use the following text and format:
You have won {number_of_rounds_won} of {number_of_rounds_played} rounds.
Leave one blank line between each round.
After the last round, print a message indicating the user’s win percentage. Ensure the percentage is formatted to zero decimal places
(example: 57%). Use the following text and format:
Your win percentage is {percentage}.
Sample Output:
Round 1
Enter your choice (1 - Rock, 2 - Paper, 3 - Scissors): 3
Computer chose Rock.
Rock beats Scissors, Computer wins!
You have won 0 of 1 rounds.
Round 2
Enter your choice (1 - Rock, 2 - Paper, 3 - Scissors): 9
Invalid choice! (1, 2 or 3): -1
Invalid choice! (1, 2 or 3): 2
Computer chose Rock.
Paper beats Rock, Player wins!
You have won 1 of 2 rounds.
Round 3
Step by Step Solution
3.37 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Program import javautilScanner public class Main public static void mainString args Scanner keyboard new ScannerSystemin declare variables to count no ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started