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

Write a \"Rock, Paper, Scissors\" game in Java. Prompt the user to enter a numbe

ID: 3842741 • Letter: W

Question

Write a "Rock, Paper, Scissors" game in Java. Prompt the user to enter a number of games to play. In order to eliminate the possibility a tie in the end, ensure that the user enters an odd number of games. The user and computer will each choose between three items: rock (defeats scissors, but loses to paper), paper (defeats rock, but loses to scissors), and scissors (defeats paper, but loses to rock.) If the user makes an invalid choice, prompt them to The computer will choose randomly which item to play. In the event that the user and computer choose the same item, there is no winner, and the round is replayed. Keep track of the number of wins each by the user and by the computer After the game has ended, print the number of wins by the user and by the computer, and declare a winner. Sample session: Welcome to Rock, Paper, Scissors! Please enter the number of rounds you would like to play: 2 Sorry, you need to enter an odd number. Please: 4 Sorry, you need to enter an odd number. Please: 3 Rock, Paper, or Scissors?: Monkey Sorry, "Monkey" is not a valid entry. Rock, Paper, or Scissors?: Rock Computer chooses Paper. You lose. Rock, Paper, or Scissors?: Rock Computer chooses Rock. It's a tie. Rock, paper, or Scissors?: Paper Computer chooses Rock. You win! Rock, Paper or Scissors?: Rock Computer chooses Scissors. You win! User wins: 2 Computed wins: 1 User Wins the game!

Explanation / Answer


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

       /* Name of the class has to be "Main" only if the class is public. */
       public class prime {
           public static void main(String[] args) throws java.lang.Exception
           {
               Scanner s = new Scanner(System. in );
               Random rnd = new Random();
               int l=0,i=0,j=0;
               String [] arr={"Rock","Paper","Scissor"};
              
               boolean x=true;
               while(x)
       {
               int B =s.nextInt();
              
if(B%2==0)
{
   System.out.println("please enter the odd number");
}
else
{
   x=false;
   l=B;
}
}
              
              
              
               while (l >= 0)
               {
                  
                   System.out.println("Rock,Paper or Scissor ?:");
                   String input=s.nextLine();
                  
                   int randomNumber = rnd.nextInt(arr.length);

                   if (arr[randomNumber].equals("Rock"))
                   {
                       if (input.equals("Rock"))
                       {
                           System.out.println("computer chooses"+" "+arr[randomNumber]+"its a tie");
                       } else if (input.equals("Paper")) {
                           i++;
                           System.out.println("computer chooses"+" "+arr[randomNumber]+" YOU WIN");
                       } else if (input.equals("Scissor")) {
                           System.out.println("computer chooses"+" "+arr[randomNumber]+"YOU LOSE");
                           j++;
                       }
                   } else if (arr[randomNumber].equals("Paper")) {
                       if (input.equals("Rock")) {
                           j++;
                           System.out.println("computer chooses"+" "+arr[randomNumber]+" YOU LOSE");
                       } else if (input.equals(" Paper")) {
                           System.out.println("computer chooses"+" "+arr[randomNumber]+"its a TIE");
                       } else if (input.equals("Scissor")) {
                           i++;
                           System.out.println("computer chooses"+" "+arr[randomNumber]+"YOU WIN");
                       }

                   } else if (arr[randomNumber].equals("Scissor")) {
                       if (input.equals("Rock")) {
                           i++;
                           System.out.println("computer chooses"+" "+arr[randomNumber]+" YOU WIN");
                       } else if (input.equals("Paper")) {
                           j++;
                           System.out.println("computer chooses"+" "+arr[randomNumber]+"YOU LOSE");
                       } else if (input.equals("Scissor")) {
                          
                           System.out.println("computer chooses"+" "+arr[randomNumber]+" its a TIE");
                       }
                   }
                   l--;

           }
               System.out.println("user wins."+" "+i);
           System.out.println("computer wins."+" "+j);
           if(i>j)
           {
               System.out.println("user wins the game!");
           }
           else
               System.out.println("computer wins the game!");
           }
       }
out put

2
please enter the odd number
3
Rock,Paper or Scissor ?:
Rock,Paper or Scissor ?:
Rock
computer chooses Paper YOU LOSE
Rock,Paper or Scissor ?:
Paper
computer chooses ScissorYOU LOSE
Rock,Paper or Scissor ?:
Rock
computer chooses Scissor YOU WIN
user wins. 1
computer wins. 2
computer wins the game!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote