452,546 views
11 votes
11 votes
Please help this is due pretty soon! (language=Java) Beginner's computer science

Assignment Details=
1. Write code for one round.
a. Get the user’s selection using a Scanner reading from the keyboard.
Let's play RPSLR!

1. Rock
2. Paper
3. Scissors
4. Lizard
5. Spock
What is your selection? 4

b. Get the computer’s selection by generating a random number.
c. Compare the user’s selection to the computer’s selection.
d. For each comparison, print the outcome of the round.
You chose Lizard.
The Computer chose Spock.
Lizard poisons Spock.
The User has won.

2. Modify your code by adding a loop.
a. Add a loop to your code to repeat each round.
b. Ask if the player wants to play again. If the player doesn’t want to play again, break out of the loop.
Do you want to play again? (Y or N) Y
3. Add summary statistics.
a. Add variables to count rounds, wins, losses, and draws and increment them
appropriately.
b. After the loop, print the summary information.
______SUMMARY_______
Rounds: 13
Wins: 5 38.5%
Loses: 7 53.8%
Draws: 1 7.7%

User Matei David
by
2.5k points

1 Answer

9 votes
9 votes

Answer:

Step-by-step explanation:

int rounds = 0;

int wins = 0;

int losses = 0;

int draws = 0;

while (true) {

// Get the user's selection

System.out.println("Let's play RPSLR!");

System.out.println("1. Rock");

System.out.println("2. Paper");

System.out.println("3. Scissors");

System.out.println("4. Lizard");

System.out.println("5. Spock");

System.out.print("What is your selection? ");

int userSelection = keyboard.nextInt();

// Get the computer's selection

int computerSelection = random.nextInt(5) + 1;

// Compare the user's selection to the computer's selection

if (userSelection == 1 && computerSelection == 3 ||

userSelection == 1 && computerSelection == 4 ||

userSelection == 2 && computerSelection == 1 ||

userSelection == 2 && computerSelection == 5 ||

userSelection == 3 && computerSelection == 2 ||

userSelection == 3 && computerSelection == 4 ||

userSelection == 4 && computerSelection == 2 ||

userSelection == 4 && computerSelection == 5 ||

userSelection == 5 && computerSelection == 1 ||

userSelection == 5 && computerSelection == 3) {

// User wins

System.out.println("The User has won.");

wins++;

} else if (userSelection == computerSelection) {

// Draw

System.out.println("It's a draw.");

draws++;

} else {

// Computer wins

System.out.println("The Computer has won.");

losses++;

}

// Ask if the player wants

User Awilkening
by
3.3k points