Well, I'm in a java class and I've got this program that I can't seem to totally figure out. I have to use methods to make a guessing game. The methods I need to make will be used to get a random number(0-100), get the person's guess, and decide whether the guess is correct and display the proper message (along with the number of guesses it took). I'll show you what I have so far, but I know it needs a lot of work.
I'm not sure if I'm doing the random thing correctly, and how I'm really returning. I don't know how to change it so that it is 0-100, other than putting it in the (), but that didn't work. Honestly, I don't think I've learned methods very well. Any input?
[ January 25, 2006: Message edited by: Savannah Aepli ] [ January 25, 2006: Message edited by: Savannah Aepli ]
My first suggestion is to put Code Tags around your code so that the indentation stays put. This will make it much easier for others to read and more likely you will get good suggestions. You can go back and edit your post by clicking on the paper/pencil icon.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Dec 06, 2001
Let's start with method:
The last line is a bit of a problem. It calls the method getARandom() which makes it "start over". Can you see that this will continue to happen forever? This is probably not what you want.
Also, you are not actually getting a randome number here. I suggest that you use the Java API docs (see the link below in my signature) to find out more information about the Random class. These docs have all the classes organized according to their package. Since Random is in the java.util class, you should start by finding that link and clicking on it. Then you can find the link for the Random class and view information about all its methods. Can you find one that will help you here?
You might want to write a small program off to the side that uses the Random class by printing out several random numbers between 0 and 100. It's always a good idea to start with small programs like this and gradually add to it until you get a program that does everything you want.
In general, things that your program "does" belong inside a method body. So if you want to call System.exit(0); you should to do so within a method.
If a method is declared to return something, then it needs a return statement. In particular, getGuess and displayMessage are both declared to return an int. If you don't want to return anything, the return type should be void.
The parameters passed in a method call need to match the method's argument list. For example, if you call displayMessage and pass 2 ints as parameters, then you need a displayMessage method defined to accept 2 ints.
Now, here's something really critical: Your getRandom method creates a new instance of Random, and then calls itself. This is an infinite loop that will keep creating new instances of Random forever. (Actually, you'll get a StackOverflowError.) This method needs to return an int. So how do you get an int from an instance of Random?