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
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")){
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
