# Random Number Guessing Game Trouble w/ Results

Page 1 of 1

## 4 Replies - 16912 Views - Last Post: 28 October 2009 - 01:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=134585&amp;s=0169a11af15ec4ec1ee8c81e7d8208bc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gingerbeard

Reputation: 0
• 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");
}

// 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...");
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++;

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

Reputation: 10
• 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++;

### #3 gingerbeard

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

## Re: Random Number Guessing Game Trouble w/ Results

Posted 26 October 2009 - 07:38 PM

Thanks...

```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?

### #4 gingerbeard

Reputation: 0
• 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 !! 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");
}

// 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...");
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++;

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

### #5 Fuzzyness

• Comp Sci Student

Reputation: 669
• 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