Ask Question
2 April, 20:05

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. 2 April, 20:32
    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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers