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

Please write a program that will implement the classic game of Rock, Paper, Scis

ID: 3577086 • Letter: P

Question

Please write a program that will implement the classic game of Rock, Paper, Scissors.

- You are required to put each functional module (such as get user type in with error checking, generate user choice and computer choice, output the result, etc.) into separate methods.

- You should error check the user input to make sure it is Rock, Paper or Scissors

- In order to simulate the computer playing, you need to generate a random number. The formula to do so is: randomNum = (int) (Math.random() * 3);The formula will generate a random number in the range 0 - 2, inclusive. You should use a switch statement or an if-else statement to assign the computer's choice according to the following: “0” corresponds to Rock, “1” corresponds to Paper, and “2” corresponds to Scissors.

-You should use two 1-D arrays to store the user's choices and the computer's choices for each round, and finally you are expected to output the game details for each round

- You should use a do/while loop to allow the user to continue playing games until ready to quit. You should ask the user if they would like to play another game and allow them to answer yes or no. You should error check to make sure the user types either yes or no and continue to prompt until yes or no is entered.

- All output displayed to the user should be done with JOptionPane.showInputDialog methods. • Make sure that when you use the strings "Rock", "Paper", or "Scissors" in your code you surround them in quotes.

-

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

public class Main {
public static void main(String args[]){

String choice, choice1;
int num = 0;
Random r = new Random();
num = r.nextInt(3);

System.out.println("Choose: Rock, Paper or Scissors. Rock : 0 Paper: 1 Scissors 2.");
Scanner userChoice = new Scanner(System.in);
choice1 = userChoice.next();
choice = choice1.toLowerCase();

switch (num){
case 0:
String num1 = "rock";
System.out.println("System chose Rock");
if (choice.matches(num1)){
System.out.println("Its a tie!");

}
else if (choice.matches("paper")){
System.out.println("You win!");

}
else if (choice.matches("scissors")){
System.out.println("You lose!");
}
else {
System.out.println();
System.out.println("ERROR: Please choose Rock, Paper or Scissors");
}
break;
case 1:
String num2 = "paper";
System.out.println("System chose Paper");
if (choice.matches(num2)){
System.out.println("Its a tie!");

}
else if (choice.matches("scissors")){
System.out.println("You win!");

}
else if (choice.matches("rock")){
System.out.println("You lose!");
}
else {
System.out.println();
System.out.println("ERROR: Please choose Rock, Paper or Scissors");
}
break;
case 2:
String num3 = "scissors";
System.out.println("System chose Scissors");
if (choice.matches(num3)){
System.out.println("Its a tie!");

}
else if (choice.matches("rock")){
System.out.println("You win!");

}
else if (choice.matches("paper")){
System.out.println("You lose!");
}
else {
System.out.println();
System.out.println("ERROR: Please choose Rock, Paper or Scissors");
}
break;


}

}
}