wiklitteku edu if and Eise do re mi (Tarno Remix)-Youube EECS188 Lab4 ITTC Creat
ID: 3585705 • Letter: W
Question
wiklitteku edu if and Eise do re mi (Tarno Remix)-Youube EECS188 Lab4 ITTC Create a program, NumberGuessingGame that will make the user guess a random number between 1 and 100 (inclusive). The game won't end unti the user guesses the comect number, but along the way you will holp the user Until the user guesses the secret number Obtain a guess ·If it's higher than the number, tol the user If it's lower than the number, tel the user If they guess the number, the game stops and they are prompted to either play again or exit Afher they win.. Congratulate them Tell them how many guesses it took Toll what their closest incorrect guess was multiplo guesses are 0qualy close, display the most recent one Use do while loops in your program Sample run (whore the secret number is 73) Welcome to the pumber guessing gane Gueas anumberi 45 sorry, you guess is too low. Guess a number: 99 sorry, your quess is too high. Guess a number: 74 Borry, your guess is too high. Guess a number i 72 Borry, your guess is too low. Guess a nunber i 73 You wint You guessed the secret number atter 5 guess(es). Your closest guess was 72. ould you 1ike to play again? (y/n)Explanation / Answer
import java.util.*;
//Class NumberGuessingGame definition
public class NumberGuessingGame
{
//Main method definition
public static void main(String[] args)
{
//Random class object created to generate random number
Random r = new Random();
//Scanner class object created to accept user guess number
Scanner sc = new Scanner(System.in);
//To store user choice y or n
char choice;
//To store computer generated random number
int computerGuess;
//To store user generated guess number
int userGuess;
//Counter to display number of guess
int counter;
//To store closest guess number
int closeGuess;
System.out.println("Welcome to the numebr guessing game.");
//Loops till user choice is not 'N' or 'n'
do
{
//Counter and close guess number is initialized to zero
counter = closeGuess = 0;
//Generates a random number between 1 and 100
computerGuess = r.nextInt(100) + 1;
//Loops till random number not equals to user guess number
do
{
//Accept a guess number from the user
System.out.println("Guess a number: ");
userGuess = sc.nextInt();
//Checks if the user guess number difference to the computer guess number is minimum then updates the close guess number
if((userGuess - computerGuess == 1) || (userGuess - computerGuess == 2)
|| (userGuess - computerGuess == -1) || (userGuess - computerGuess == -2))
closeGuess = userGuess;
//Increase the counter by one
counter++;
//Checks if the user guess number is less than the computer random number then display message low
if(userGuess < computerGuess)
System.out.println("Sorry, you guess is too low. ");
//Checks if the user guess number is greater than the computer random number then display message high
if(userGuess > computerGuess)
System.out.println("Sorry, you guess is too high. ");
//Checks if the user guess number is equal to the computer random number then display the result
else if(userGuess == computerGuess)
{
System.out.println("You win! ");
System.out.println("You guessed the secret number after " + counter + " guess(es). ");
System.out.println("Your closest guess was " + closeGuess);
break;
}//End of else if
}while(true);//End of inner do - while loop
//Accept user choice for next round
System.out.println("Would you like to play again(y/n): ");
choice = sc.next().charAt(0);
//Checks if the user choice is 'N' or 'n' then stop
if(choice == 'N' || choice == 'n')
//Come out of the loop
break;
}while(true);//End of outer do - while loop
}//End of main method
}//End of class
Sample Run:
Welcome to the numebr guessing game.
Guess a number: 90
Sorry, you guess is too high.
Guess a number: 80
Sorry, you guess is too low.
Guess a number: 85
Sorry, you guess is too low.
Guess a number: 87
Sorry, you guess is too low.
Guess a number: 88
You win!
You guessed the secret number after 5 guess(es).
Your closest guess was 87
Would you like to play again(y/n): y
Guess a number: 50
Sorry, you guess is too high.
Guess a number: 40
Sorry, you guess is too high.
Guess a number: 30
Sorry, you guess is too high.
Guess a number: 20
Sorry, you guess is too high.
Guess a number: 10
Sorry, you guess is too low.
Guess a number: 15
Sorry, you guess is too low.
Guess a number: 18
Sorry, you guess is too high.
Guess a number: 17
You win!
You guessed the secret number after 8 guess(es).
Your closest guess was 18
Would you like to play again(y/n): n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.