In: Computer Science
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
//GuessGame.java import java.util.Random; import java.util.Scanner; public class GuessGame { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int num, secretNumber, count = 0; Random rand = new Random(); secretNumber = 1+rand.nextInt(100); System.out.println("A random number was generated from 1 to 100."); while(true) { System.out.print("Please enter your guess: "); num = scanner.nextInt(); count++; if(secretNumber < num){ System.out.println("too high"); } else if(secretNumber > num){ System.out.println("too low"); } else{ System.out.println("good news"); System.out.println("Number of guesses: "+count); break; } } } }