34,774 views
26 votes
26 votes
Please help with coding from a beginner's computer sci. class (Language=Java)

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 Justin Solms
by
2.8k points

1 Answer

15 votes
15 votes

Answer: Here is some sample code that demonstrates how to complete the assignment using Java:

import java.util.Random;

import java.util.Scanner;

public class RPSLR {

public static void main(String[] args) {

// Initialize scanner for reading user input

Scanner scanner = new Scanner(System.in);

// Initialize random number generator for computer's selection

Random random = new Random();

// Initialize counters for rounds, wins, losses, and draws

int rounds = 0;

int wins = 0;

int losses = 0;

int draws = 0;

// Main game loop

while (true) {

// Get 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 = scanner.nextInt();

// Get computer's selection

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

// Compare selections and determine outcome

String outcome;

if (userSelection == computerSelection) {

outcome = "draw";

draws++;

} else 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)) {

outcome = "win";

wins++;

} else {

outcome = "lose";

losses++;

}

// Print outcome of round

String userSelectionString;

String computerSelectionString;

if (userSelection == 1) {

userSelectionString = "Rock";

} else if (userSelection == 2) {

userSelectionString = "Paper";

} else if (userSelection == 3) {

userSelectionString = "Scissors";

} else if (userSelection == 4) {

userSelectionString = "Lizard";

} else {

userSelectionString = "Spock";

}

if (computerSelection == 1) {

computerSelectionString = "Rock";

} else if (computerSelection == 2) {

computerSelectionString = "Paper";

} else if (computerSelection == 3) {

computerSelectionString = "Scissors";

} else if (computerSelection == 4) {

computerSelectionString = "Lizard";

} else {

computerSelectionString = "Spock";

}

User Rodrigo Cavalcante
by
2.6k points