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

QUESTION: Modify the program so that the user is asked at the end if they want t

ID: 3592868 • Letter: Q

Question

QUESTION: Modify the program so that the user is asked at the end if they want to play again. Repeat until they say "no" or "NO"

Please see java code of what i have completed. Please show results once program successfully runs. Thank you

import java.util.Random;

       import java.util.Scanner;

       public class looping

       {

           /**

           * This is a method header. It is used to describe what a method does

           * This is the main method. It is the starting point of the program

           *   

           * @param args

           */

           public static void main(String[] args)

           {

               // declare variables

               int userChoice;

               int computerChoice;

               String winner = "";

              

               // initialize random number generator and scanner

               Random generator = new Random();

               Scanner input = new Scanner(System.in);

              

              

               // create random generated computer choice

               computerChoice = generator.nextInt(3) + 1;

              

               // get user choice

               System.out.println("Please choose a number between 1 and 3");

               userChoice = input.nextInt();

              

               // determine the winner

              if ((computerChoice == 2 && userChoice == 1) ||

                       (computerChoice==3 && userChoice == 2) ||

                       (computerChoice == 1 && userChoice == 3) ||

                       (computerChoice == 2 && userChoice == 3))

               {

                   // the four ways user can win

                   winner = "User";

               }

               else

               {

                   winner = "Computer";

               }

              

              // Tell the user what happened

              System.out.println("You chose " + userChoice + ", The computer chose " + computerChoice);

              if (winner.equals("User"))

              {

                 System.out.println("YOU WIN");

              }

              else

              {

                 System.out.println("Sorry, Comupter wins");

              }

              System.out.println("Would you like to play again? Enter P to play again ");

          String playAgain = input.nextLine();

           while (!playAgain.equals("P") && !playAgain.equals("p")){

          

              

           }

       }

       }

Explanation / Answer

looping.java


import java.util.Random;
import java.util.Scanner;
public class looping
{

/**
* This is a method header. It is used to describe what a method does
* This is the main method. It is the starting point of the program
*
* @param args
*/
public static void main(String[] args)
{
// declare variables
int userChoice;
int computerChoice;
String winner = "";
  
// initialize random number generator and scanner
Random generator = new Random();
Scanner input = new Scanner(System.in);
String playAgain = "yes";
while (!playAgain.equalsIgnoreCase("no")){   
// create random generated computer choice
computerChoice = generator.nextInt(3) + 1;
  
// get user choice
System.out.println("Please choose a number between 1 and 3");
userChoice = input.nextInt();
  
// determine the winner
if ((computerChoice == 2 && userChoice == 1) ||
(computerChoice==3 && userChoice == 2) ||
(computerChoice == 1 && userChoice == 3) ||
(computerChoice == 2 && userChoice == 3))
{
// the four ways user can win
winner = "User";
}
else
{
winner = "Computer";
}
  
// Tell the user what happened
System.out.println("You chose " + userChoice + ", The computer chose " + computerChoice);
if (winner.equals("User"))
{
System.out.println("YOU WIN");
}
else
{
System.out.println("Sorry, Comupter wins");
}
System.out.println("Would you like to play again? (NO to exit): ");
playAgain = input.next();
  
}
}
}

Output:

Please choose a number between 1 and 3
2
You chose 2, The computer chose 3
YOU WIN
Would you like to play again? (NO to exit):
yes
Please choose a number between 1 and 3
1
You chose 1, The computer chose 3
Sorry, Comupter wins
Would you like to play again? (NO to exit):
yes
Please choose a number between 1 and 3
2
You chose 2, The computer chose 3
YOU WIN
Would you like to play again? (NO to exit):
no

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