I'm having some trouble with printing certain results in my guessing game. We're required to have a guessing game where the program/computer "thinks of" an integer and the user guesses what the number is until (s)he gets it right. For each incorrect guess, it tells the user whether the answer is higher or lower. Main method must contain a while loop that plays multiple games and prompts the user for whether or not to play another game. I've gotten this down.
Your program is required to have the following static methods in addition to the main method:
1. a method to give instructions to the user <-- I've got this.
2. a method that plays one game with the user (only one!) <-- I've got this.
3. a method that reports the overall results to the user<-- I'm having trouble here.
This is where I'm stuck (underlined):
At the end of play, your program will report various statistics about the games played by the user. Report the total number of games played, the total number of guesses made (all games included), the average number of guesses per game, and the maximum number of guesses made in any single game.
I'll try to create a totalGuesses variable and do totalGuesses += numGuesses, but I only get 1 when I do this. I think it may have something to do with passing variables? Maybe I need another method? Here's the code, it should execute.
import java.util.*; public class Guess { // class constant from 1 to int MAX public static final int MAX = 100; public static void main(String[] args) { Scanner console = new Scanner(System.in); // intializes variables int userGuess = 0; int compPick = 0; int numGuesses = 1; // gives user instructions and range of guessing giveInstructions(); // plays 1 game with the user playGame(console, userGuess, compPick, numGuesses); // plays again until user enters something that doesn't begin with 'y' or 'Y' int numGames = 1; System.out.print("Do you want to play again? "); String playAgain = console.next(); playAgain = playAgain.toLowerCase(); while (playAgain.charAt(0) == 'y') { playGame(console, userGuess, compPick, numGuesses); System.out.print("Do you want to play again? "); playAgain = console.next(); playAgain = playAgain.toLowerCase(); numGames++; // adds total number of games } // prints results printResults(numGames); } // instructions on how to play public static void giveInstructions() { System.out.println("This program allows you to play a guessing game."); System.out.println("I will think of a number between 1 and " + MAX); 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."); } // plays the game public static void playGame(Scanner console, int userGuess, int compPick, int numGuesses) { System.out.println(); System.out.println("I'm thinking of a number..."); System.out.print("Your guess? "); userGuess = console.nextInt(); Random generator = new Random(); compPick = generator.nextInt(MAX) + 1; numGuesses = 1; while (userGuess != compPick) { if (userGuess < compPick) { System.out.println("higher"); } else if (userGuess > compPick) { System.out.println("lower"); } numGuesses++; System.out.print("Your guess? "); userGuess = console.nextInt(); } System.out.println("You got it right in " + numGuesses + " guesses"); System.out.println(); } // prints results public static void printResults(int numGames) { System.out.println(); System.out.println("Overall results: "); System.out.println("\ttotal games = " + numGames); // prints total number of games System.out.println("\ttotal guesses = "); // prints overall number of guesses throughout all games played System.out.println("\tguesses/game = "); // prints average guesses per game System.out.println("\tmax guesses = "); // prints the highest number of guesses for each game } }
Any help will be greatly appreciated!