Random Number Guessing Game Trouble w/ Results

I've got the program running, and am now having trouble with calcu

Page 1 of 1

4 Replies - 16396 Views - Last Post: 28 October 2009 - 01:50 AM Rate Topic: -----

#1 gingerbeard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 09

Random Number Guessing Game Trouble w/ Results

Posted 26 October 2009 - 04:36 PM

Hi all, first post on here. :) Thanks for everything in advance. I'll try to be clear about what I'm looking for.

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Random Number Guessing Game Trouble w/ Results

#2 phil1000   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Random Number Guessing Game Trouble w/ Results

Posted 26 October 2009 - 05:05 PM

you need to do the totalGuesses += numGuesses when you make a guess so around the time you do this: numGuesses++;
Was This Post Helpful? 0
  • +
  • -

#3 gingerbeard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 09

Re: Random Number Guessing Game Trouble w/ Results

Posted 26 October 2009 - 07:38 PM

Thanks...

But when I add this:
int totalGuesses = 0;
totalGuesses += numGuesses;



right outside of the while loop in the playGame static method, it only returns the same number as numGuesses, instead of adding the total guesses throughout every game that is played. I think that's the right idea, but how do I add all of them up every time without losing anything?
Was This Post Helpful? 0
  • +
  • -

#4 gingerbeard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 09

Re: Random Number Guessing Game Trouble w/ Results

Posted 27 October 2009 - 08:14 PM

Ah, still need help with this counting thing :angry: !! OK, I've added the totalGuesses variable, but when I attempt to print it in the last method, I only get 0, which is what it was declared to in the main method. How do I get it to go through every method, and add up each time? I tried a return statement but to no avail. I also made a do while loop as opposed to calling the playGame method and the a while loop. I feel like I'm forgetting something very easy.

Here's my code:

import java.util.*;

public class Guess 
{
	// class constant to guess from 1 - 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;
		int totalGuesses = 0;
		int numGames = 0;
		
		// gives user instructions and range of guessing
		giveInstructions();
		
		// plays 1 game with the user
		String playAgain = "";
		do {
		   playGame(console, userGuess, compPick, numGuesses, totalGuesses);
		   
		   System.out.print("Do you want to play again? ");
		   
		   playAgain = console.next().toLowerCase();
		} while (playAgain.charAt(0) == 'y'); 
		
			numGames++; // adds total number of games

		// prints results
		printResults(numGames, totalGuesses);  
	}
	
	// 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, int totalGuesses) {
				
		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();
		}
		
		totalGuesses += numGuesses;
		
		System.out.println("You got it right in " + numGuesses + " guesses");		
	}

	// prints results
	public static void printResults(int numGames, int totalGuesses) {
		System.out.println();
		System.out.println("Overall results: ");
		System.out.println("\ttotal games   = " + numGames); // prints total number of games
		System.out.println("\ttotal guesses = " + totalGuesses); // prints overall number of guesses throughout all games played
		System.out.println("\tguesses/game  = " + (totalGuesses / numGames)); // prints average guesses per game
		System.out.println("\tmax guesses   = "); // prints the highest number of guesses for each game
	}
}


This post has been edited by gingerbeard: 27 October 2009 - 08:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Random Number Guessing Game Trouble w/ Results

Posted 28 October 2009 - 01:50 AM

Have you tried moving the totalGuesses variable outside of the main method? I think what is happening is that it is only increasing inside the method because it was sent through the method call. However because the variable was created in the main method, it cannot be increased from another method. In order to be able to increase it in another method you would need to have the variable belong to the class and not the main method. Which would mean you move it out of the main method.

Hope this helps!


Edit - Look into Scope if confused.

This post has been edited by Fuzzyness: 28 October 2009 - 01:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1