Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im trying to write a program in javs that randomly generates a number. Then, the

ID: 3725791 • Letter: I

Question

Im trying to write a program in javs that randomly generates a number. Then, the user is to guessing the number. Please explain in Detail.

The command line Java program to input, keep track of, and check user's attempts to guess a randomly generated number. The input portion of the program will be a continuous loop, only breaking out of it when the user either guesses the correct number, or by entering -1 to signify giving up. After each guess, the user will be informed of his guess being too high, too low, or exactly correct. Upon exiting the loop, the user is given feedback of their guesses (amount guessed too low, amount guessed too high, total amount of guesses).

I want to use :

import java.util.Random;
Random rand = new Random();
int value = rand.nextInt(32)+1

This is how the output is supposed to look like:

Guess the number I'm thinking of, from 1-100: 21
Your guess is larger than the random value. Next guess:
5
Your guess is larger than the random value. Next guess:
7
Your guess is smaller than the random value. Next guess:
4
You've guessed correct!
Total number of guesses: 4
Smaller Guesses: 1

Explanation / Answer

Please fidn my code.

import java.util.*;

public class Guess {

   public static final int MAX = 100;

   public static void main(String[] args) {

       gameIntro();

       Scanner console = new Scanner(System.in);

       Random ronWeasley = new Random();

       int answer;

       String yStandard = "y";

       int game = 1;

       int numGuesses = 0;

       //System.out.println("I'm thinking of a number between 1 and " + MAX + "...");

       while (yStandard.equalsIgnoreCase("y")) {

           game++;

           answer = ronWeasley.nextInt(MAX) + 1;

           System.out.println("Answer: "+answer);

           numGuesses = numGuesses + playGameOne(console, answer);

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

           yStandard = console.next();

       }

       overallResults(game, numGuesses);

   }

   //

   public static void gameIntro() {

       System.out.println("This program allows you to play a guessing game.");

       System.out.println("I will think of a number between 1 and");

       System.out.println("100 and will allow you to guess until");

       System.out.println("you get it. For each guess, I will tell you");

       System.out.println("whether the right answer is higher or lower");

       System.out.println("than your guess.");

   }

   //

   public static int playGameOne(Scanner console, int answer) {

       int numGuesses = 0;

       int guess = -1;

       System.out.println("I'm thinking of a number between 1 and " + MAX + "...");

       while (guess != answer) {

           System.out.print("Your guess? ");

           guess = console.nextInt();

           numGuesses++;

           if (guess > answer) {

               System.out.println("It's lower.");

           } else

               System.out.println("It's higher.");

       }

       if (numGuesses == 1) {

           System.out.println("You got it right in 1 guess");

       } else

           System.out.println("You got it right in " + numGuesses + " guesses");

       return numGuesses;

   }

   //

   public static void overallResults(int game, int numGuesses) {

       System.out.println("Overall results:");

       System.out.println(" total games = " + game);

       System.out.println(" total guesses = " + game);

       System.out.println(" guesses/game = " + round1(numGuesses/game));

       System.out.println(" best game = " + numGuesses);

   }

   //This method returns the result of rounding

   //n to 1 digit after the decimal point.

   public static double round1(double n) {

       return Math.round(n * 10.0) / 10.0;

   }

}

/*

Sample run:

This program allows you to play a guessing game.

I will think of a number between 1 and

100 and will allow you to guess until

you get it. For each guess, I will tell you

whether the right answer is higher or lower

than your guess.

Answer: 100

I'm thinking of a number between 1 and 100...

Your guess? 80

It's higher.

Your guess? 90

It's higher.

Your guess? 95

It's higher.

Your guess? 100

It's higher.

You got it right in 4 guesses

Do you want to play again?

y

Answer: 74

I'm thinking of a number between 1 and 100...

Your guess? 74

It's higher.

You got it right in 1 guess

Do you want to play again?

n

Overall results:

total games = 3

total guesses = 3

guesses/game = 1.0

best game = 5

*/