Question: I'm doing a Java programming assignment working with while loops where it plays a number guessing game. The program includes the option to play as

I'm doing a Java programming assignment working with while loops where it plays a number guessing game. The program includes the option to play as many games until you choose and once you chose not too it gives you end game statistics. One of those statistics is "best game" which prints the lowest number of guesses it took. For example if you played 4 games with 5,6,7,5 guesses it will print 5. I'm unsure on how to add this into my program or how to separate the counts or if I need to add an additional count. I posted a copy of my code below.

10 import java.util.*; 11 12 public class GuessingGame { 13 public static final int MAX = 100; 14 15 public static void main(String[] args) { 16 Scanner console = new Scanner(System.in); 17 Random rand = new Random(); 18 int num; 19 String playAgain = "y"; 20 int game = 0; 21 int count = 0; 22 23 guessIntro(); 24 25 while (playAgain.equalsIgnoreCase("y") || playAgain.equalsIgnoreCase("yes")) { 26 game++; 27 num = rand.nextInt(MAX) + 1; 28 count = count + playGame(console, num); 29 System.out.print("Do you want to play again? "); 30 playAgain = console.next(); 31 System.out.println(); 32 } 33 overallResults(game, count); 34 35 } 36 37 public static int playGame(Scanner console, int num) { 38 39 // int guessMin = 0; 40 int count = 0; 41 int guess = -1; 42 System.out.println("I'm thinking of a number between 1 and " + MAX + "..."); 43 44 45 while(guess != num) { 46 System.out.print("Your guess? "); 47 guess = console.nextInt(); 48 count++; 49 if(guess > num) { 50 System.out.println("It's lower."); 51 } 52 else if(guess < num) { 53 System.out.println("It's higher."); 54 } 55 } 56 if(count == 1) { 57 System.out.println("You got it right on the first guess!!"); 58 } 59 else { 60 System.out.println("You got it right in " + count + " guesses"); 61 } 62 return count; 63 } 64 65 public static void guessIntro() { 66 System.out.println("This program allows you to play a guessing game."); 67 System.out.println("I will think of a number between 1 and"); 68 System.out.println(MAX +" and will allow you to guess until"); 69 System.out.println("you get it. For each guess, I will tell you"); 70 System.out.println("whether the right answer is higher or lower"); 71 System.out.println("than your guess."); 72 System.out.println(); 73 } 74 75 public static void overallResults(int game, int count) { 76 double average = count*1.0/game; 77 int guessMin = 0; 78 if(count > guessMin) { 79 guessMin = count; 80 } 81 System.out.println("Overall results: "); 82 System.out.println("\ttotal games = " + game); 83 System.out.println("\ttotal guesses = " +count); 84 System.out.printf("\tguesses/game = %,.1f" , average); 85 System.out.println(); 86 System.out.println("\tbest game = " +guessMin); 87 } 88 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!