Ultramixer pro entertain5/4/2023 Let's tackle the first item on that list and get rid of it once and for all. Keeps track of number of guesses (remember we only finished part of it).Great, we can cross of another requirement off of our list. This is what your code should look like at this point: 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). 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. If the number guessed is the same as the real number, tell us that we won.If the number guessed is smaller than the real number, tell us its too low.If the number guessed is higher than the real number, tell us its too high.That's a big clue as to what you need to use to accomplish this. Notice how in that sentence I used the word if. The computer then needs to tell us if this guess was too high or too low. Once you've written the code to accept input, you can scratch that off of your requirements list. Do you remember what to use instead for integers? Also, remember that using nextLine() with Scanner probably isn't the best approach here. Remember, the player will be guessing integers, so having the variable be an integer is a must. Now with this variable, under where the computer asks for input, have your new variable store the input from the scanner. You can create this variable at the top too, but don't make it equal anything yet. Now that we have a scanner to use, we need to actually have a variable that stores the input from the user. You know how to create a Scanner by now, right? :) So, create a Scanner at right under your variable that keeps track of the number of guesses. 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. 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.
0 Comments
Leave a Reply. |