• Game rule: – paper beats rock; rock beats scissor and scissor beats paper. – P
ID: 3731168 • Letter: #
Question
• Game rule:
– paper beats rock; rock beats scissor and scissor beats paper.
– Paper=0; rock = 1; scissor = 2
– Computer generates random number 0 – 2
• ( int ) ( Math.random ()*3)
– You are asked to enter value 0-2
– Use at least one switch statement
– DETERMINE WHO WINS OR TIE BECAUSE BOTH PICKED SAME VALUE
– PRINT – what computer picked, what you picked and who won
• “Computer picked rock, I picked paper, I won”
• “Computer picked rock, I picked rock, tie”
• EXTRA CREDIT - ONLY USE SWITCH STATEMENTS
Explanation / Answer
RockPaperScissors.java
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
//Declaring variables
int userChoice;
String 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
* the user enters a valid string
*/
while (true) {
//Getting the input entered by the user
System.out.println(" Choose :");
System.out.println("0.Rock");
System.out.println("1.Paper");
System.out.println("2.Scissors");
System.out.print("Enter Choice :");
userChoice = sc.nextInt();
//Calling the computer Selection method
int computerChoice = ComputerSelection();
switch (userChoice) {
case 0:
{
System.out.print(", I picked rock,");
switch (computerChoice) {
case 0:
{
winner = " tie";
tie++;
break;
}
case 1:
{
winner = " Computer Wins!";
computerWinsCnt++;
break;
}
case 2:
{
winner = " I Won.";
playerWinsCnt++;
break;
}
}
break;
}
case 1:
{
System.out.print(", I picked paper,");
switch (computerChoice) {
case 0:
{
winner = " I win!";
playerWinsCnt++;
break;
}
case 1:
{
winner = " tie";
tie++;
break;
}
case 2:
{
winner = " Computer Wins!";
computerWinsCnt++;
break;
}
}
break;
}
case 2:
{
System.out.print(", I picked scissors,");
switch (computerChoice) {
case 0:
{
winner = " Computer Wins.";
computerWinsCnt++;
break;
}
case 1:
{
winner = " I Wins!";
playerWinsCnt++;
break;
}
case 2:
{
winner = " tie";
tie++;
break;
}
}
break;
}
default:
{
System.out.println("** Invalid Inputs **");
break;
}
}
System.out.print(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 int ComputerSelection() {
String str;
int computerInt = ( int ) ( Math.random ()*3);
System.out.print("Computer picked ");
switch (computerInt) {
case 0:
str = "rock";
System.out.print(str);
break;
case 1:
str = "paper";
System.out.println(str);
break;
case 2:
str = "scissors";
System.out.print(str);
break;
}
return computerInt;
}
}
___________________
Output:
Choose :
0.Rock
1.Paper
2.Scissors
Enter Choice :0
Computer picked rock, I picked rock, tie
Do you want to continue(Y/N) ::y
Choose :
0.Rock
1.Paper
2.Scissors
Enter Choice :2
Computer picked rock, I picked scissors, Computer Wins.
Do you want to continue(Y/N) ::y
Choose :
0.Rock
1.Paper
2.Scissors
Enter Choice :1
Computer picked rock, I picked paper, I win!
Do you want to continue(Y/N) ::y
Choose :
0.Rock
1.Paper
2.Scissors
Enter Choice :0
Computer picked scissors, I picked rock, I Won.
Do you want to continue(Y/N) ::n
No of times User won :2
No of times User loss :1
No of times Game tie :1
:: Program Exit ::
_________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.