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

Write a program that plays the Rock-Paper-Scissors game against the computer. Wh

ID: 3837213 • Letter: W

Question

Write a program that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of the three options (usually shown by a hand gesture) at the same time and a winner is determined. Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it), then prompt for the user's selection. At that point, the program reveals both choices and prints a statement indicating whether the user won, the computer won, or there was a tie. Play continues until the user chooses to stop; final output tells the number of user wins, losses, and ties.

Explanation / Answer

RockPaperScissors.java

import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
  
//Declaring variables
String userChoice="",winner="";
int computerWinsCnt=0,playerWinsCnt=0,tie=0;
  
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
  
//This while loop continues to execute until winner is decided
while(true)
{
  
/* This while loop continues to execute until
* the user enters a valid string
*/
while(true)
{
//Getting the input entered by the user
System.out.print(" Choose rock, paper, scissors?: ");
userChoice=sc.next();
  
/* If the user entered String is either "rock" or "paper" or "scissors"
* then the while loop will break
* If not,it will prompt the user to enter again
*/
if(userChoice.equals("rock") || userChoice.equals("paper") || userChoice.equals("scissors"))
{
break;
}
else
{
//Displaying error message
System.out.println("Invalid Input.");
continue;
}
  
}
//Calling the computer Selection method
String computerChoice=ComputerSelection();
  
//Finding who won in the game
if (userChoice.equals("rock") && computerChoice.equals("scissors")){
  
winner="Rock smashes scissors, you win!";   
playerWinsCnt++;
}
else if(userChoice.equals("scissors") && computerChoice.equals("rock")){
winner="Rock smashes scissors, Computer Wins!";
computerWinsCnt++;
}
else if(userChoice.equals("rock") && computerChoice.equals("paper")){
winner="Paper covers rock, Computer Wins!";
computerWinsCnt++;
}
else if(userChoice.equals("paper") && computerChoice.equals("rock")){
winner="Paper covers rock, you win!";
playerWinsCnt++;
}
else if(userChoice.equals("paper") && computerChoice.equals("scissors")){
winner="Scissors cut paper,Computer Wins!";
computerWinsCnt++;
}
else if(userChoice.equals("scissors") && computerChoice.equals("paper")){
winner="Scissors cut paper, you win!";
playerWinsCnt++;
}
else{
winner="tie";
tie++;

}
  
//Displaying the winner
System.out.println(" "+winner);
  
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
char ch = sc.next(".").charAt(0);
if(ch=='Y'||ch=='y')
continue;
else
{
   //Displaying the no of times user won and computer won or tie
   System.out.println("No of times User won :"+playerWinsCnt);
   System.out.println("No of times User loss :"+computerWinsCnt);
   System.out.println("No of times Game tie :"+tie);
System.out.println(" :: Program Exit ::");
break;
}

}
  
}

//This method will chooses the computer selection randomly
private static String ComputerSelection() {
String str="";
Random rand=new Random();
int computerInt=1 + rand.nextInt((3 - 1) + 1);
System.out.print("Computer Choice: ");
switch (computerInt) {
case 1:
str="rock";
System.out.print(str);
break;
case 2:
str="paper";
System.out.print(str);
break;
case 3:
str="scissors";
System.out.print(str);
break;
}
return str;
}
}

________________________

Output:


Choose rock, paper, scissors?: paper
Computer Choice: paper
tie
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: rock
Computer Choice: scissors
Rock smashes scissors, you win!
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: rock
Computer Choice: scissors
Rock smashes scissors, you win!
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: paper
Computer Choice: paper
tie
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: scissors
Computer Choice: rock
Rock smashes scissors, Computer Wins!
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: paper
Computer Choice: rock
Paper covers rock, you win!
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: rock
Computer Choice: rock
tie
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: paper
Computer Choice: scissors
Scissors cut paper,Computer Wins!
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: rock
Computer Choice: scissors
Rock smashes scissors, you win!
Do you want to continue(Y/N) ::y

Choose rock, paper, scissors?: paper
Computer Choice: scissors
Scissors cut paper,Computer Wins!
Do you want to continue(Y/N) ::n
No of times User won :4
No of times User loss :3
No of times Game tie :3

:: Program Exit ::

______________Thank You

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