View Full Version : [Java] Help with Number Guessing Game

03-01-2012, 11:43 PM
I'm gonna go ahead and include my code for the entire program, but there's only one part I'm having trouble on - and this is for homework so hints only please!

It's a pretty simple number guessing game, where I report back to the user at the end of each game how many guesses it took them to guess the random number the computer was "thinking of." I've got that all figured out. My problem comes in toward the end - I have to ask the user if they want to play again, over and over until they don't, and that's also find. In the end, however, I need to print some overall statistics for all the games played, including the total number of guesses in all games, the total number of games played, and the best game (aka, the one that they took the fewest number of guesses to get) identified by number of guesses.

Since playing the game is in a do-while loop, I can't figure out how to get to it save the number of guesses for each and every game played, and then compare them to report back what was the fewest number of guesses required.

Also, I can't figure out how to return the value for total number of games played (I have it tallying that number okay, just can't figure out how to get it out of the loop in the very end).

Any ideas would be much appreciated!

import java.util.*;

public class NumberGuessLoop {

public static final int UPPER_LIMIT = 100;

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int totalGuesses = 0;
totalGuesses = game(keyboard, rand, totalGuesses);
int totalGames = 1; // need to figure out how to tally number of games and do
// more than one game
results(totalGuesses, totalGames);


* introduction -- This method introduces the user to the program and what
* will occur in the game.
public static void introduction() {
System.out.println("This program plays the number guessing game.");
System.out.println("I'll randomly choose a number between 1 and "
+ UPPER_LIMIT + ", and you try to guess");
System.out.println("that number. After each guess, I'll tell you if "
+ "my number is higher or");
System.out.println("lower than your guess, if you were incorrect. "
+ "Then you keep guessing");
System.out.println("until you've guessed my number. Once you've "
+ "guessed it, I'll let you");
System.out.println("know how many guesses it took, and ask if you"
+ " would like to play again.");
System.out.println("Let's start now!");

* game -- This method will execute one game with the user.
* @param keyboard
* -- allows for user input
* @param rand
* -- allows for random number generation
* @return numberGuesses -- the number of guesses it took the user to guess
* the number
public static int game(Scanner keyboard, Random rand, int totalGuesses) {
String playAgain;
int totalGames = 0;
do {
System.out.println("Okay, I am thinking of a number between 1 & "
+ UPPER_LIMIT + ".");
int number = rand.nextInt(100) + 1;
System.out.println("random number is " + number); // comment out later
int guess;

int numberGuesses = 0;
do {
System.out.println("Your guess?");
guess = keyboard.nextInt();
if (guess < 1 || guess > UPPER_LIMIT) {
System.out.println("Your guess must be in the range 1-"
+ UPPER_LIMIT + ". Try again.");
} else if (guess != number) {
System.out.println("Sorry, that guess is incorrect.");
if (guess < number) {
.println("The number I am thinking of is higher.");
} else {
.println("The number I am thinking of is lower.");
} while (guess != number);
.println("Yes, the number I was thinking of was " + guess);
.println("You found it in " + numberGuesses + " guesses.");
System.out.println("Do you want to play another game? (Y|N)");
playAgain = keyboard.next();
} while (playAgain.toLowerCase().charAt(0) == 'y');

return totalGuesses;

* results -- This method prints the results of all the games played.
* @param totalGuesses
* @param totalGames
public static void results(int totalGuesses, int totalGames) {
System.out.println("total games = " + totalGames);
System.out.println("total guesses = " + totalGuesses);
System.out.println("guesses/game = " + (totalGuesses / totalGames));
.println("best game = " /* + lowest returned number of guesses */);



03-02-2012, 12:22 AM
Are you familiar with collections? They are like arrays but are dynamic (arrays need to be resized and are always explicit).
Collections are easy to use for what you need. All you need to do is insert an entry into a collection (declared and initialized before the do/while loop) representing the number of guesses. The number of items in the collection (from .size()) refers to the number of games played, and the value associated with an offset represents the guesses it took.
For an example:

ArrayList<Integer> cGameGuesses = new ArrayList<Integer>();
cGameGuesses.add(4); // First game took four guesses
cGameGuesses.add(8); // Second game took 8 guesses.
// etc
System.out.println("Played a total of " + cGameGuesses.size() + " games");
int i = 0;
for (Integer iTookGuesses : cGameGuesses)
System.out.println("Game: " + ++i + " took " + iTookGuesses);

For an example.