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

Add a loop that continuously prompts the user for the number, indicating whether

ID: 3654902 • Letter: A

Question

Add a loop that continuously prompts the user for the number, indicating whether the guess is high or low, until the user enters the correct value. After the user correctly guesses the number, display a count of the number of attempts it took. *Enhance the game so the player has to pick a number between 1 and 1,000. * Once the player has figured out the correct number, ask if they would like to play again using the Scanner class. If yes, restart the game with a new random number. If not, use the System.out.println(); method to display a message that thanks the user for playing the game.Save the file as RandomGuess3.java.

Explanation / Answer

package com.cramster.com; import java.util.Random; import java.util.Scanner; public class GuessRandonNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random random = new Random(); int number; int count; int loop; while(true) { number = random.nextInt(1000) + 1; count =0; while(true) { System.out.printf("Guess the number: (1 to 1000): "); int guessNumber = input.nextInt(); if(guessNumber < 1 || guessNumber > 1000) System.out.printf("Wrong Input. Number has to be between 1 and 1000 "); else if(guessNumber == number) { count++; System.out.printf("Congratulations!! Your guess is correct "); System.out.printf("It took you %d guesses to guess the correct answer ",count); break; } else { count++; System.out.printf("Wrong guess. Your guess is %s Try again ", guessNumber > number ? "higher" : "Lower"); } } System.out.printf("Do you want to repeat the game? "); System.out.printf("Press 0 to quit or 1 to try again "); loop = input.nextInt(); if(loop == 0) break; } System.out.printf("Program Terminated."); } }
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