Ask Question
27 August, 22:41

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.

+5
Answers (1)
  1. 28 August, 01:18
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers