Question
Im working on a Java guessing game and its all done except for the calculation to figure out the game with the most guesses...I just cant figure out how to calculate that. The game with the most guesses should appear next to "max guesses = ____" in the overallResults method. Heres my code:
import java.util.*;
public class CheggQuestionAnswered {
public static int RANGE = 100;
public static int tries;
public static int totalTries = 1;
public static int totalGames = 0;
public static void main(String[] args) {
boolean wantsToPlay = true;
Scanner scanner = new Scanner(System.in);
introduction();
do {
playgame();
System.out.print("Do you want to play again? ");
String choice = scanner.next();
if (choice.charAt(0) == 'y' || choice.charAt(0) == 'Y')
{
wantsToPlay = true;
} else {
wantsToPlay = false;
}
} while (wantsToPlay);
overallResults();
}
public static void introduction () {
System.out.println("This program allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and 100");
System.out.println("and will allow you to guess until you get it.");
System.out.println("For each guess, I will tell you whether the");
System.out.println("right answer is higher or lower than your guess.");
System.out.println();
}
public static void playgame() {
boolean playing = true;
Random rand = new Random();
int generatedNumber = rand.nextInt(RANGE);
int tries = 1;
System.out.println();
System.out.println("I'm thinking of a number...");
while (playing) {
Scanner input = new Scanner(System.in);
System.out.print("Your guess? ");
int guess = input.nextInt();
if(guess < generatedNumber) {
System.out.println("higher");
tries++;
totalTries++;
}
else if(guess > generatedNumber) {
System.out.println("lower");
tries++;
totalTries++;
}
else if(guess == generatedNumber) {
System.out.println("You got it right in " + tries + " guesses");
System.out.println();
totalGames++;
break;
}
}
}
public static void overallResults() {
System.out.println();
System.out.println("Overall Results:");
System.out.println(" total games = " + totalGames);
System.out.println(" total guesses = " + totalTries);
System.out.println(" guesses/game = " + (double)totalTries / (double)totalGames);
System.out.println(" max guesses = ");
}
}
Explanation / Answer
import java.util.*; public class GameManager { public static final int MAX_GUESS = 100; private int numGames; // number of games played private int totGuesses; // total number of guesses made private int maxGuesses; // highest number of guesses made private double average; // average number of guesses made /**------------------------------------------------------------------------ * GameManager constructor * * Initialize class variables to starting values, no games yet */ public GameManager() { numGames = totGuesses = maxGuesses = 0; } /**------------------------------------------------------------------------ * play * * Allow the user to play as many number guessing games as desired. * Consists of displaying game rules, playing one game, figuring and * reporting results. */ public void play() { Scanner keyboard = new Scanner(System.in); int guesses; // number of guesses user needs for a game displayGameIntro(); // display game rules String playAgain; // whether to play again // keep playing games as long as the user wants to play do { guesses = playGame(keyboard); // play one game // compute out statistics: total, average, and max guesses figureStats(guesses); System.out.print("Do you want to play again? "); playAgain = keyboard.next(); System.out.println(); } while (playAgain.toUpperCase().startsWith("Y")); reportResults(); // display statictics } /**------------------------------------------------------------------------ * displayGameIntro * * Displays the introduction of the game to the user */ private void displayGameIntro() { System.out.println("This program allows you to play a guessing game."); System.out.println("I will think of a number between 0 and " + MAX_GUESS); System.out.println("and will allow you to guess until you get it."); System.out.println("For each guess, I will tell you whether the"); System.out.println("right answer is higher or lower than your guess."); System.out.println(); } /**------------------------------------------------------------------------ * playGame * * Plays one game: prompts user to enter a number and then determines * if it is higher or lower than the correct answer. * At the end of the games, displays how many guesses it took. */ private int playGame(Scanner keyboard) { Random r = new Random(); int answer = r.nextInt(MAX_GUESS); int guess = answer - 1; int numGuesses = 0; // number of guesses user needs for a game System.out.println("I'm thinking of a number..."); // keep letting the user guess numbers until the correct // number is guessed; tell user if the correct answer is // higher or lower than the number they guess do { // prompt user, get their guess, and count it System.out.print("Your guess? "); guess = keyboard.nextInt(); numGuesses++; // figure out whether higher or lower if (guess answer) { System.out.println("lower"); } } while (guess != answer); System.out.println("You guessed it!"); // display how many guesses were needed return numGuesses; } /**------------------------------------------------------------------------ * figureStats * * Figure out total number of guesses, number of games, and max guesses. */ private void figureStats(int guesses) { totGuesses += guesses; numGames++; if (guesses > maxGuesses) { maxGuesses = guesses; } } /**------------------------------------------------------------------------ * reportResults * * Displays the overall results including total games played, * total number of guesses, average number of guesses, and max guesses. */ private void reportResults() { System.out.println("Overall results:"); System.out.println(" total games = " + numGames); System.out.println(" total guesses = " + totGuesses); average = (double) totGuesses / numGames; System.out.println(" guesses/game = " + average); System.out.println(" max guesses = " + maxGuesses); } }