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

I\'m writing a code for a random number guessing game. I basically have everythi

ID: 658279 • Letter: I

Question

I'm writing a code for a random number guessing game. I basically have everything figured out except I need the whole thing to run as a while loop so that at the end I can prompt the player to answer whether they want to play again ("Y/N") and then it'll restart the program from the beginning but I don't know the best way to do that now. My current code is below. as you can see I already have put it in a while loop but I don't know the best way to go about finishing it. I also need to create an overall results report that gives - number of games played, total guesses, average guesses per game, and the best game the player played (game with least amount of guess):

import java.util.*; //so I can use scanner

public class GuessingGame {
public static void main(String[] args) {

Random rand = new Random ();
int max = 1000;
int numberToGuess = rand.nextInt(max) + 1;
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
boolean playAgain = true;
  
while (playAgain == true) {
  
System.out.println("I'm thinking of a number between 1 and " + max + "...");

while (win == false) {

System.out.print("Your guess? " + numberToGuess);
guess = input.nextInt();
numberOfTries++;
  
if (guess == numberToGuess) {
win = true;
} else if (guess > numberToGuess) {
System.out.println("It's lower.");
} else if (guess < numberToGuess) {
System.out.println("It's higher.");
}
}
if (numberOfTries == 1) {
System.out.println("You got it right in " + numberOfTries + " guess!");
} else {
System.out.println("You got it right in " + numberOfTries + " guesses!");
}

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

}
}
}
}

This assignment focuses on while loops and random numbers. Turn in a file named GuessingGame.java. Your program allows the user to play a game in which the program thinks of a random integer and accepts guesses from the user until the user guesses the number correctly. After each incorrect guess, you will tell the user whether the correct answer is higher or lower. Your program must exactly reproduce the format and behavior of the logs in this document. The log below shows one sample execution of your program. Your output w ill differ depending on the random numbers chosen and user input typed, but the overall output structure should match that shown below. First, the program prints an introduction in the form of a haiku poem. Recall that a haiku has 3 lines: one with 5 syllables, the second with 7 syllables, and the third with 5 syllables. Next, a series of guessing games is played. In each game, the computer chooses a random number between 1 and 100 inclusive. The game asks the user for guesses until the correct number is guessed. After each incorrect guess, the program gives a clue about whether the correct number is higher or lower than the guess. Once the user types the correct number, the game ends and the program reports how many guesses were needed. After each game ends and the number of guesses is shown, the program asks the user

Explanation / Answer

Here is the code for the question. Sample output also shown. Please rate the answer if it helped. Thank you very much.

import java.util.*; //so I can use scanner

public class GuessingGame {

   public static void main(String[] args) {

       Random rand = new Random ();

       int max = 100;

       int numberToGuess = rand.nextInt(max) + 1;

       int totalTries=0,numberOfTries = 0, numOfGames=0, bestGuess=-1;

       Scanner input = new Scanner(System.in);

       int guess;

       boolean win = false;

       boolean playAgain = true;

       String ans;

       while (playAgain == true) {

           numberToGuess = rand.nextInt(max) + 1;

          

           numberOfTries = 0;

           win = false;

           numOfGames++;

          

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

           while (win == false) {

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

               guess = input.nextInt();

               numberOfTries++;

               if (guess == numberToGuess) {

                   win = true;

               } else if (guess > numberToGuess) {

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

               } else if (guess < numberToGuess) {

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

               }

           }

           if (numberOfTries == 1) {

               System.out.println("You got it right in " + numberOfTries + " guess!");

           } else {

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

           }

           totalTries += numberOfTries;

           if(bestGuess == -1 || numberOfTries < bestGuess)

               bestGuess = numberOfTries;

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

           ans = input.next();

           if(ans.equalsIgnoreCase("y") || ans.equalsIgnoreCase("yes"))

               playAgain = true;

           else

               playAgain =false;

       }

      

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

       System.out.println("Total games = "+numOfGames);

       System.out.println("Total guesses = "+totalTries);

       System.out.println("Guesses/game = "+ 1.0 * totalTries/numOfGames);

       System.out.println("Best guess = "+bestGuess);

   }

}

output

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

Your guess? 40

It's higher.

Your guess? 60

It's lower.

Your guess? 50

It's higher.

Your guess? 55

It's lower.

Your guess? 53

It's lower.

Your guess? 51

You got it right in 6 guesses!

Do you want to play again? y

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

Your guess? 80

It's lower.

Your guess? 60

It's lower.

Your guess? 40

It's lower.

Your guess? 20

It's lower.

Your guess? 10

It's lower.

Your guess? 5

It's higher.

Your guess? 8

It's lower.

Your guess? 7

You got it right in 8 guesses!

Do you want to play again? y

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

Your guess? 30

It's higher.

Your guess? 50

It's higher.

Your guess? 70

It's higher.

Your guess? 90

It's higher.

Your guess? 97

You got it right in 5 guesses!

Do you want to play again? n

Overall results:

Total games    = 3

Total guesses    = 19

Guesses/game    = 6.333333333333333

Best guess    = 5