Java Guessing Game In this guessing game, you will create a program in which the computer will come up with a random number between 1 and 1000. The player must then continue to guess numbers until the player guesses the correct number. For every guess, the computer will either say "Too high" or "Too low", and then ask for another input. At the end of the game, the number is revealed along with the number of guesses it took to get the correct number. First create a new Java Project called Games, Next, we're going to start by creating a new class, or Java file. Call your new program GuessingGame, keeping the capitalization the same. 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. You should start out like this: Ok, we need the computer to generate random numbers. Just add this code inside your main method so you have this: The variable numbertoguess holds the integer that the player has to guess. Now, we need to figure out exactly what we need our game to do and how we're going to accomplish this goal. Let's start by listing what the guessing game needs to do, also known as the requirements of the program. Needs of the guessing game: Creates a random number to guess
Keeps track of number of guesses Asks us to guess a number Let user input a number Tells us whether we're guessing too high or too low Keeps playing until we guess the correct number Tells us the correct number and the number of tries We already took care of the first need, which was to create a random number. So, let's move on to the next requirement, keeping track of the number of guesses. To keep track of anything, you need a variable. In this case, since we're keeping track of guesses, a simple integer variable will do. Add an int variable to your code, and start it off at 0, since at the beginning the player has made no guesses. So far we have the variable, but it still does not keep track of the number of guesses because the user isn't being asked to make any guesses yet. Let's have the computer ask us to guess a number.
Now, we need the player to be able to input the number. This means we're going to need a Scanner. I suggest you try to define all variables as high up in the code as possible. It helps you keep track of all the different variables you have and makes sure that you don't accidentally use the same variable twice. So, create a Scanner at right under your variable that keeps track of the number of guesses. You should know how to create a Scanner Now that we have a scanner to use, we need to actually have a variable that stores the input from the user. You can create this variable at the top too.
Have your new variable store the input from the scanner. Remember, the player will be guessing integers, so having the variable be an integer is a must. Remember to use nextint() with Scanner probably. The computer then needs to tell us if this guess was too high or too low. Notice how in that sentence the word if is emphasized. That's a big clue as to what you need to use to accomplish this. Let's break down the if statement. If the number guessed is higher than the real number, tell us it s too high. If the number guessed is smaller than the real number, tell us it s too low. If the number guessed is the same as the real number, tell us that we won. You could do this in three different if statements, but it's best to use else if in this case to tie them all together. It helps to show that all those if's are related to each other, and that only one of those if's will ever be true at one time (the guessed number can never be too high and too low at the same time, for example). This is what your code should look like at this point: Keeps track of number of guesses (remember we only finished part of it)
Keeps playing until we guess the correct number Tells us the correct number and the number of tries We should track the number of guesses Right after the player guesses the number. Run your program now and notice what happens. The program would go one time, and then stop. This is because we need it to keep going until the user wins. We will have to think about this a little bit before we code it. First of all, what ways do we know to make Java do something over and over again? In the class we went over two ways, the for loop and the while loop. If you remember the difference, then you know that the for loop loops for a certain number of times. Unfortunately the number of times this program could loop depends on the player! So, a for loop is probably not the best way to handle this.
A while loop is the perfect choice. It keeps going until a condition is no longer true. So, while the player hasn't won yet, keep going. But how do we keep track of whether or not the player has won? We use a boolean variable. We can create a boolean variable called win near the top of our code with all the other variables. If we set win to false, then it means the player hasn't won yet. When win is true, then the player won the game. The last thing we need to figure out is which code to put inside of this while loop. I recommend everything starting from the computer asking the player to guess a number all the way down to the if statements. See how inside the while loop parenthesis the condition is when win is equal to false? This means it will continue to loop until something sets the win variable to true.
But what will set the win variable to true? The third part of the if statement seems like a good choice. The part that asks if the player guessed the correct number. Here is what this looks like: Now we need the game statistics when the game is over. Ok, after your while loop, we can add the code in. It should just be a series of println that tell us everything we need to know, such as number of guesses made. Your guessing game program is now complete! Go ahead and play it. I wouldn't try guessing letters or anything like that, as your program will crash.