Create a game of Rock Paper Scissors using random import a w
Create a game of Rock, Paper, Scissors using random import, a while loop, and a series of if statements. The user should input either “rock”, “paper”, or “scissors”- and the output should print the computers choice, as well as the result of the match. If the match is tied, it should automatically start a new match. For example:
Example 1:
rock, paper, scissors?: rock
Computer chose scissors.
Rock smashes scissors, you win!
Example 2:
rock, paper, scissors?: paper
Computer chose scissors.
Scissors cut paper, you lose!
Example 3:
rock, paper, scissors?: scissors
Computer chose scissors.
rock, paper, scissors?: rock
Computer chose paper.
Paper covers rock, you lose!
Solution
RockPaperScissors.java
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
//Declaring variables
String userChoice=\"\",winner=\"\";
//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(\"\ 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();
//Calling the decide winner method
winner=DecideWinner(userChoice,computerChoice);
if(winner.equals(\"tie\"))
continue;
else
break;
}
//Displaying the winner
System.out.println(\"\ \"+winner);
}
//This method will decide the winner and return the string to the caller
private static String DecideWinner(String userChoice, String computerChoice) {
String winner=null;
if (userChoice.equals(\"rock\") && computerChoice.equals(\"scissors\")){
winner=\"Rock smashes scissors, you win!\";
}
else if(userChoice.equals(\"scissors\") && computerChoice.equals(\"rock\")){
winner=\"Rock smashes scissors, you lose!\";
}
else if(userChoice.equals(\"rock\") && computerChoice.equals(\"paper\")){
winner=\"Paper covers rock, you lose!\";
}
else if(userChoice.equals(\"paper\") && computerChoice.equals(\"rock\")){
winner=\"Paper covers rock, you win!\";
}
else if(userChoice.equals(\"paper\") && computerChoice.equals(\"scissors\")){
winner=\"Scissors cut paper, you lose!\";
}
else if(userChoice.equals(\"scissors\") && computerChoice.equals(\"paper\")){
winner=\"Scissors cut paper, you win!\";
}
else{
winner=\"tie\";
}
return winner;
}
//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;
}
}
___________________________________
Output1:
rock, paper, scissors?: rock
Computer Choice: paper
Paper covers rock, you lose!
______________________________
Output2:
rock, paper, scissors?: paper
Computer Choice: rock
Paper covers rock, you win!
______________________________
Output3;
rock, paper, scissors?: scissors
Computer Choice: rock
Rock smashes scissors, you lose!
__________________________
output4:
rock, paper, scissors?: paper
Computer Choice: paper
rock, paper, scissors?: rock
Computer Choice: paper
Paper covers rock, you lose!
_______Thank Yoiu


