I like to define all my variables as high up in the code as possible, and I suggest you try to do the same. This means we're going to need a Scanner. Now, we need the player to be able to input the number. Ok, so that requirement is completely done and you can scratch it off your to-do list. This is pretty simple, and something you've known how to do since the Hello World tutorial if you've been following along. Let's have the computer ask us to guess a number. At this point it doesn't make sense to make it do so because the user isn't being asked to make any guesses yet. So far we have the variable, but it still does not keep track of the number of guesses. Add an int variable to your code, and start it off at 0, since at the beginning the player has made no guesses. In this case, since we're keeping track of guesses, a simple integer variable will do. To keep track of anything, you need a variable. So, let's move on to the next requirement, keeping track of the number of guesses. We already took care of the first need, which was to create a random number. This is a small list, but it does say everything we need to do for our guessing game to work. Tells us the correct number and the number of tries.Keeps playing until we guess the correct number. Tells us whether we're guessing too high or too low.It's best to do this planning BEFORE you beign coding, so let's start by listing what the guessing game needs to do, also known as the requirements of the program. Now, we need to stop and figure out exactly what we need our game to do and how we're going to accomplish this goal. If you want to just import manually, type in import at the very top of the page. This will take care of the problem for you. All you have to do is right-click your work area, go to source, and select Organize Imports. This is the same problem that Scanner has. Notice you'll get an error when you try to use Random. All you need to know for this guessing game is that the variable numberToGuess holds the integer that the player has to guess. Just add this code inside your main method so you have this:ĭon't worry much about how Random works. Ok, we need the computer to generate random numbers. If you're using Eclipse (and I strongly urge you to!) then make sure to checkmark the box to have it put in your main method for you. Call your new program GuessingGame, keeping the capitalization the same. Ready to follow along to create this guessing game? All right.įirst, we're going to start by creating a new class, or Java file. At the end of the game, the number is revealed along with the number of guesses it took to get the correct number. For every guess, the computer will either say "Too high" or "Too low", and then ask for another input. The player must then continue to guess numbers until the player guesses the correct number. In this guessing game, the computer will come up with a random number between.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |