in java, write a program that generates a random number and asks the user to gue
ID: 3659463 • Letter: I
Question
in java, write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." if the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number. .Your output may look like: I'm thinking of a number from 0 till 9. Guess what it is: 5 No, that's too low. Guess again: 7 No, that's too low. Guess again: 8 Congratulations! You guessed it! I was thinking of the number 8.Explanation / Answer
import java.util.Scanner; import java.util.Random; public class NumberGuess { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random generator = new Random(); int myNumber = generator.nextInt((99)+1); System.out.println("I'm thinking of a number from 0 to 99 - enter a guess: "); int count = 0; String result = ""; boolean done = false; while (!done) { count++; int guess = input.nextInt(); if (guess > myNumber) { result = "Too high, try again."; System.out.println(result); done = false; } else if (guessRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.