Answer:
import java.util.Scanner; //to accept input from user
import java.util.Random; //to generate random numbers
public class Main { //class name
public static void main(String[] args) { //main method
System.out.println("Please select one of [R/P/S]: "); //prompts user to select a choice
Scanner scanner = new Scanner(System.in); //reads input choice
String playerChoice = scanner.next().toUpperCase().replace(" ", ""); //converts the choice to upper case and stores it in playerChoice
if (playerChoice.equals("R") || playerChoice.equals("P") || playerChoice.equals("S")) { // if player choice is a valid choice
results(CompChoice(), playerChoice); // calls method to compute result of game
} else { //if player enters invalid choice
System.out.println("\\Invalid choice!"); } } //error message
private static String CompChoice() { //to generate computer choice
Random generator = new Random(); //to generate random numbers
int comp = generator.nextInt(3)+1; //generates 3 random numbers
String compChoice = ""; //to store computer choice
switch (comp) { //checks the random number for corresponding computer choice
case 1: //1 is for Rock
compChoice = "Rock"; //sets compChoice to Rock
break;
case 2: //random number 2 is for Paper
compChoice = "Paper"; //sets compChoice to Paper
break;
case 3: //random number 3 is for Scissors
compChoice = "Scissors"; //sets compChoice to Scissors
break; }
return compChoice; } //returns computer choice
private static void results(String compChoice, String playerChoice) { //method to compute result
if(playerChoice.equals("R")) //if playerChoice is equal to R
{ playerChoice = "Rock"; //stores Rock to playerChoice
System.out.println("You chose: Rock");} //displays player choice
else if(playerChoice.equals("P")) //if playerChoice is equal to P
{ playerChoice = "Paper"; //stores Paper to playerChoice
System.out.println("You chose: Paper");} //displays player choice
else //if playerChoice is equal to S
{ playerChoice="Scissors"; //stores Scissors to playerChoice
System.out.println("You chose: Scissors"); } //displays player choice
System.out.println("I chose: " + compChoice); //displays computer choice
if (compChoice.equals(playerChoice)) { //if compChoice is equal to playerChoice
System.out.println("a tie!"); //its a tie if computer and player choice is the same
System.exit(1); }//exits on tie
boolean youWin = false; //variable to check if player wins
if (compChoice.equals("Rock")) { //if computer choice is Rock
if (playerChoice.equals("Paper")) //if player choice is Paper
{System.out.println("Paper beats Rock.");
youWin = true;} //player wins to youWin is set to true
else if(playerChoice.equals("Scissors")) { //if player choice is Scissors
System.out.println("Rock beats scissors.");
youWin = false;} } //player loses to youWin is set to false
if (compChoice.equals("Paper")) { //if computer choice is Paper
if (playerChoice.equals("Rock")) //if player choice is Rock
{System.out.println("Paper beats Rock.");
youWin = false;} //player loses to youWin is set to false
else if(playerChoice.equals("Scissors")) { //if player choice is Scissors
System.out.println("Scissors beats Paper.");
youWin = true;} } //player wins to youWin is set to true
if (compChoice.equals("Scissors")) { //if computer choice is Scissors
if (playerChoice.equals("Rock")) //if player choice is Rock
{System.out.println("Rock beats Scissors.");
youWin = true;} //player wins to youWin is set to true
else if(playerChoice.equals("Paper")) { //if player choice is Paper
System.out.println("Scissors beats Paper.");
youWin = false;} } //player loses to youWin is set to false
if (youWin) //if player wins means if youWin is true
System.out.println("you win!"); //declare player to be winner
else //if player loses means if youWin is false
System.out.println("you lose!"); } } //declare player to lose
Step-by-step explanation:
The program is well explained in the comments attache with each line of program. The program has three methods. One is the main method to start the game and prompts user to enter a choice and converts the user input choice to upper case. One is CompChoice method that generates three random numbers and uses switch statement to set 1 for Rock, 2 for Paper and 3 for Scissors . One method is results that takes computer choice and player choice as parameters to decide the winner. It uses if else statements to check if choice of user wins from choice of computer or vice versa. It uses a boolean variable youWin which is set to true when player choice beats computer choice otherwise is set to false. At the end if youWin is true then the player is declared winner otherwise not. The screenshot of the program output is attached.