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. You shall also keep a count of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses. Now add another loop to ask the user if he or she wishes to play the guessing game again. If so, the loop should repeat, otherwise it should terminate.So far, this is what my program looks like, any help or suggestions would be greatly appreciated!// This program generates a random number and askes the user to guess what the number isimport java.util.Scanner;import java.util.Random;public class GuessingGame{public static void main(String [] args){//Create a scanner object to read from the keyboardScanner kb = new Scanner(System.in);//Create a random objectRandom rand = new Random();//Identifier declarationsint num = rand.nextInt(100) + 1;int guess = 0;int count = 0;int guesses = 0;do{System.out.println("Guess what number I have (1-100)? ");guess = kb.nextInt();guesses ++;if(num > guess) {System.out.println("Too high, try again.");} else if(num < guess) {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);}}

Respuesta :

Answer:

import java.util.Scanner;

import java.util.Random;

public class Guesser {

// Create a scanner object to read from the keyboard

static Scanner kb = new Scanner(System.in);

// Create a random object

static Random rand = new Random();

public static void main(String[] args) {

 playGame();

}

public static void playGame() {

 // Identifier declarations

 int num = rand.nextInt(100) + 1;

 int guess = 0;

 int count = 0;

 int guesses = 0;

 do {

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

  guess = kb.nextInt();

  guesses++;

  if (num > guess) {

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

  } else if (num < guess) {

   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");

   //Ask the user if they want to play again

   //If yes, recursively call the playGame method again

   //Otherwise, break out of the loop

   System.out.println("Do you want to play again? (Y or N)");

   String answer = kb.next();

   if (answer.equalsIgnoreCase("Yes") || answer.equalsIgnoreCase("Y")) {

    playGame();

   }

   else {

    break;

   }

  }

 } while (guess != num);

}

}

Explanation:

There are a few things you need to do.

1. Make the creation of the objects of the Scanner and Random classes global. In other words, make them global variables.

2. Create a method, say playGame(), that is called, recursively, every time the user needs to play or replay the game.  

The source code file (Guesser.java) has been attached to this answer.

Note: You might want to consider also reducing the range of guess. I'd rather you reduced it to 1-10 rather than what you have. This will make the game much of a fun. With the range you currently have, the user might never guess right.

Hope it helps!

Following are the program to check the random number range:

Program Explanation:

  • Import package.
  • defining a class GuessingGame, and inside a class main method is declared.
  • Inside the method, a Scanner class and Random class object is created.
  • After creating class object an integer variable "num" is defined that hold a random value.
  • In the next step, two integer variable "guess and count"and a boolean variable "done" is declared.
  • To check random number a while loop is define that use a boolean variable and define a condition to check the range of random number and print its related message.

Program:

import java.util.Scanner;//import package

import java.util.Random;//import package

public class GuessingGame //defining a class GuessingGame

{

public static void main(String[] args)//defining main method  

{

Scanner input = new Scanner(System.in);//creating Scanner class object

Random generator = new Random();//creating Random class object

int num = generator.nextInt((100)+1);//defining an integer variable that hold Random number value

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

int guess = 0, count = 0;//defining integer variable

boolean done = false;//defining a boolean variable

while (!done)//defining a while loop that check input value

{

count++;//incrementing count variable value

guess = input.nextInt();//using guess variable to hold random number value  

if (guess > num)//using if to check the random number range

{

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

done = false;//holding boolean value

}

else if (guess < num)//using else if block that check the random number range

{

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

done = false;//holding boolean value

}

else if (guess == num)//using the else block that check random number range

{

System.out.println("You got it in " + count + " tries! The number was " + num + ".");//print message

done = true;//holding boolean value

}

}

}

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/14035547

Ver imagen codiepienagoya

Otras preguntas