Ask Question
12 November, 09:23

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.

+3
Answers (1)
  1. 12 November, 09:54
    0
    import java. util. Scanner;

    import java. util. Random;

    public class GuessingGame

    {

    public static void main (String [] args)

    {

    Scanner kb = new Scanner (System. in);

    Random rand = new Random ();

    int num;

    int guess = 0;

    int guesses = 0;

    char yorn;

    String in;

    do

    {

    num = rand. nextInt (100) + 1;

    guesses = 0;

    do

    {

    System. out. println ("Guess what number I have (1-100) ? ");

    guess = kb. nextInt ();

    guesses + +;

    if (guess > num) {

    System. out. println ("Too high, try again.");

    } else if (guess< num) {

    System. out. println ("Too low, try again.");

    } else {

    System. out. println ("You're right, the number is " + num);

    System. out. println ("You guessed " + guesses + " times");

    }

    }

    while (guess!=num);

    in=kb. nextLine ();

    System. out. print ("Play again (Y/N) ? ");

    in=kb. nextLine ();

    yorn=in. charAt (0);

    }while (yorn=='Y'||yorn=='y');

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Engineering if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers