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

In Chapter 5, you created a Rock Paper Scissors game. In the game, a player ente

ID: 3830406 • Letter: I

Question

In Chapter 5, you created a Rock Paper Scissors game. In the game, a player entered a number to represent one of the three choices. Make the following improvements to the game:

Allow the user to enter a string (“rock”, “paper”, or “scissors”) instead of a digit. l Make sure the game works correctly whether the player enters a choice in uppercase or lowercase letters or a combination of the two.

To allow for player misspellings, accept the player’s entry as long as the first two letters are correct. (In other words, if a player types “scixxrs”, you will accept it as “scissors” because at least the first two letters are correct.)

When the player does not type at least the first two letters of the choice correctly, reprompt the player and continue to do so until the player’s entry contains at least the first two letters of one of the options.

Allow 10 complete rounds of the game. At the end, display counts of the number of times the player won, the number of times the computer won, and the number of tie games.

Save the file as RockPaperScissors2.java.

Explanation / Answer

Here is the code for you:

import java.io.*;
import java.util.Scanner;


//public enum Choice {ROCK, PAPER, SCISSOR};
public class RockPaperScissorWithStatsAndStrings
{
  
//Choice ch;
public static void main(String[] args)
{
//Choice ch;
char again;
int numOfGames = 0, numOfWins = 0, numOfTies = 0, numOfLoses = 0;
do
{
Scanner in = new Scanner(System.in);
int computerNumber = (int)(Math.random() * 3) + 1;
System.out.print("Rock, Paper, Scissor: ");
numOfGames++;
String userInput = in.next();
userInput = userInput.substring(0, 2);
//System.out.println(userInput);
userInput = userInput.toUpperCase();
//System.out.println(userInput);
//System.out.println(computerNumber);
switch(computerNumber)
{
case 3:
           System.out.print("The computer is Scissor. ");
           if(userInput.equals("PA"))
           {System.out.println("You are a Paper. You lost"); numOfLoses++;}
           else if(userInput.equals("SC"))
           {System.out.println("You are Scissor too. It is a draw."); numOfTies++;}
           else if(userInput.equals("RO"))
           {System.out.println("You are a Rock. You won."); numOfWins++;}
           break;   
case 1:
           System.out.println("The computer is Rock. ");
           if(userInput.equals("SC"))
           {System.out.println("You are Scissor. You lost."); numOfLoses++;}
           else if(userInput.equals("RO"))
           {System.out.println("You are a Rock too. Its a draw."); numOfTies++;}
           else if(userInput.equals("PA"))
           {System.out.println("You are a Paper. You won."); numOfWins++;}
           break;
case 2:
           System.out.println("The computer is Paper. ");
           if(userInput.equals("SC"))
           {System.out.println("You are Scissor. You won."); numOfWins++;}
           else if(userInput.equals("RO"))
           {System.out.println("You are a Rock. You lost."); numOfLoses++;}
           else if(userInput.equals("PA"))
           {System.out.println("You are a Paper too. Its a draw."); numOfTies++;}
           break;
default:
           System.out.println("Invalid input.");          
}
System.out.print("Do you want to play again (Y)es / (N)o: ");
again = in.next().charAt(0);
}while(Character.toLowerCase(again) == 'y');
System.out.println("Your total games are: " + numOfGames);
System.out.println("Your total number of wins: " + numOfWins);
System.out.println("Your total number of loses: " + numOfLoses);
System.out.println("Your total number of draws: " + numOfTies);
}
}