Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the game Rock Paper Scissors, two players simultaneously choose one of three

ID: 3120413 • Letter: I

Question

In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: Rock beats scissors, because a rock can break a pair of scissors. Scissors beats paper, because scissors can cut paper. Paper beats rock, because a piece of paper can cover a rock Create a game in which the computer randomly chooses rock, paper, or scissors. Let the user enter a number 1, 2, or 3, each representing one of the three choices. Then, determine the winner. Save the application as RockPaperScissors.java. (In the chapter Characters, Strings, and the StringBuilder, you will modify the game so that the user enters a string for rock, paper, and scissors, rather than just entering a number.)

Explanation / Answer

import java.util.Scanner; import java.util.Random; public class Rock { public static void main(String[] args) { String personPlay; String computerPlay = ""; int computerInt; //computer's play String response; Scanner scan = new Scanner(System.in); Random generator = new Random(); System.out.println( "Please enter a move. " + "Rock = 1, Paper" + "= 2, and Scissors = 3."); System.out.println(); //Generate computer's play (0,1,2) computerInt = generator.nextInt(3)+1; //Translate computer's randomly generated play to //string using if //statements if (computerInt == 1) computerPlay = "1"; else if (computerInt == 2) computerPlay = "2"; else if (computerInt == 3) computerPlay = "3"; //Get player's play from input-- note that this is System.out.println("Enter your play: "); personPlay = scan.next(); //Make player's play uppercase for ease of comparison personPlay = personPlay.toUpperCase(); System.out.println("Computer play is: " + computerPlay); //See who won. Use nested ifs if (personPlay.equals(computerPlay)) System.out.println("It's a tie!"); else if (personPlay.equals("1")) if (computerPlay.equals("2")) System.out.println("Rock crushes scissors. You win!!"); else if (computerPlay.equals("1")) System.out.println("Paper eats rock. You lose!!"); else if (personPlay.equals("2")) if (computerPlay.equals("3")) System.out.println("Scissor cuts paper. You lose!!"); else if (computerPlay.equals("1")) System.out.println("Paper eats rock. You win!!"); else if (personPlay.equals("3")) if (computerPlay.equals("2")) System.out.println("Scissor cuts paper. You win!!"); else if (computerPlay.equals("1")) System.out.println("Rock breaks scissors. You lose!!"); else System.out.println("Invalid user input."); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote