In: Computer Science
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 (hint: Math.random()) chooses rock, paper, or scissors. Let the user enter "rock", "paper" or "scissor". Then, determine the winner.
Save the application as RockPaperScissors.java.
input code:
output:
code:
import java.util.*;
public class RockPaperScissors
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
Random r=new Random();
/*print the menu*/
System.out.println("\n");
System.out.println("ROCK PAPER SCISSORS MENU");
System.out.println("1. Rock");
System.out.println("2. Paper");
System.out.print("3. Scissor\nEnter your choice:");
/*take input*/
String userChoice=s.next();
/*generate number*/
int computerChoice = r.nextInt() % 3 + 1;
/*check computerChoice and display according to it*/
if (computerChoice == 1)
{
/*if computerChoice==1 than print this*/
System.out.println( "Computer: Rock");
}
else if (computerChoice == 2)
{
/*if computerChoice==2 than print this*/
System.out.println( "Computer : Paper");
}
else if (computerChoice == 3)
{
/*if computerChoice==3 than print this*/
System.out.println( "Computer : Scissors");
}
System.out.println( "User : "+userChoice);
/*check the winner*/
/*if userChoice 1*/
if (userChoice.equals("Rock"))
{
/*than check computerChoice and print according to it*/
if(computerChoice==3)
{
System.out.println( "User win !!!");
}
else if(computerChoice==2)
{
System.out.println("Computer win !!!");
}
else
{
System.out.println("Tie !!!");
}
}
else if (userChoice.equals("Paper"))
{
/*than check computerChoice and print according to it*/
if(computerChoice==3)
{
/*print this if User win*/
System.out.println( "Computer win !!!");
}
else if(computerChoice==1)
{
/*print this if Computer win*/
System.out.println("User win !!!");
}
else
{
/*print this if same*/
System.out.println("Tie !!!");
}
}
else if (userChoice.equals("Scissor"))
{
/*than check computerChoice and print according to it*/
if(computerChoice==2)
{
/*print this if User win*/
System.out.println( "User win !!!");
}
else if(computerChoice==1)
{
/*print this if Computer win*/
System.out.println("Computer win !!!");
}
else
{
/*print this if same*/
System.out.println("Tie !!!");
}
}
}
}