I'm trying to create a Guessing Game.
The game randomize a number between 1 and 100 and let's the user guess.
When correct, program asks if player want to play a new game or return to menu. While this is happening the program will also count the guesses and the number of games played, for statistics.

The menu contains of:
1. Play
2. Statistics
3. Exit

I've got the game and exit part working alright. It is the statistics that is giving me a hard time.
The statistics contains of:
Number of games played
The lowest number of guesses
The highest number of guesses
The avarage number of guesses

The avarage is no problem. NumberOfGuesses / NumberOfGames
The problem is the lowest and highest number of guesses. The program must keep track of my best game and my worst game, even if i play 50 games.

Here is where i'm stuck.
And i'm not sure that my main class is correct, looks awefully empty. It works though.

if (exit.equals ("YES"))
System.out.println("The game has ended. Please come again!");System.exit(0);

if (exit.equals ("NO")) showMainMenu();

}//End Method

//End Class
}

10-16-2013, 12:50 AM

Fou-Lu

Dangit, I had such a nice long one typed up here; stupid back mouse button >.>. Remind me to come back in case I forget since these mark them as read.
This looks like homework, so I can't give the answer, but can provide ideas; its quite simple:
Make a running least and most amount of guesses as a property to the class. When verifying and completing the game, check and see if this game score is better/worse than one of them, and if so, simply replace it. Most of it's in the playGame() method, but you'll need to do the one for statistics to show them as well.

10-16-2013, 12:08 PM

enm1l

Solved it!

Thank you for the help, it was the best =).

As u said, simple. I just forgot to initialize numberOfGuesses to 0 everytime I start a new game.

10-16-2013, 03:07 PM

enm1l

Alright, i'm back with a new problem. Still the same program.

I should implement the menu as a method showMainMenu() that returns an integer depending on the user's choice. The method showMainMenu() is found in the GAME class.
And here is what i got so far:

When i run this, it seems to be working. The game plays out fine and the statistics show a friendly error message if numberOfGames == 0. The problem is, that when i now use showMainMenu(); anywhere in my code, i get an error.

The menu pops up like it should, but any choice i make just give me errors.
I think it has something to do with that the menu just returns an integer and i can't seem to make my methods go back to main after they are executed.

Anyone have an idea? And as previous poster wrote, this is an assignment. So i would enjoy tips but not the entire solution.