In: Computer Science
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow the user to continue playing until they chooses to stop. Once they stop, print the number of user wins, losses, and ties.
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
char ch;
do {
System.out.println("Enter your choice \n1.Rock\n2.Sicssor\n3.Paper");
n=sc.nextInt();
int i=ThreadLocalRandom
.current()
.nextInt(1, 3 + 1);
if(i==1)
{
System.out.println("The computer choice is Rock.");
}
else if(i==2)
{
System.out.println("The computer choice is Sicssor.");
}
else
{
System.out.println("The computer choice is Paper.");
}
if(n==1)
{
if(i==1)
{
System.out.print("Its a tie.");
}
else if(i==2)
{
System.out.print("You won the game.");
}
else
{
System.out.print("Computer won the game.");
}
}
else if(n==2)
{
if(i==1)
{
System.out.print("Computer won the game.");
}
else if(i==2)
{
System.out.print("Its a tie.");
}
else
{
System.out.print("You won the game.");
}
}
else
{
if(i==1)
{
System.out.print("You won the game.");
}
else if(i==2)
{
System.out.print("Computer won the game.");
}
else
{
System.out.print("Its a tie");
}
}
System.out.print("\nDo you want to continue (Y/N): ");
ch=sc.next().charAt(0);
}while(ch=='y'||ch=='Y');
}
}
Here is the above program that asks the user to enter the choice of rock/paper/choice and then we the computer will generate the number by computer generator and then according to the question conditions we have Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.Then we will print who is the winner and then we ask for the user to continue or not is user enter y then we play game again or else we stop the game.
SCREENSHOT OF THE OUTPUT :