Random Number Game
RANDOM NUMBER GAME - Now it is time to put all your new knowledge to the test. You are going to build a random number game. - The game needs to generate a random number between 1 and 100. - The game needs to ask you to pick a number. - The game will let you know if the number is higher or lower than the number you picked. - If you get the number right, the game will tell you that you have won.
1 2 3 RANDOM NUMBER GAME PROJECT - Op en Eclipse - Create a new project called "NumberGame". - Create a new Java class called NumberGame. Make sure to check the box to add the "public static void main" method. - Add the code to import java.util.scanner for collecting user input. 4 - Add the code to import java.util.random for generating random numbers. 5
RANDOM NUMBER GAME PROJECT (CONTINUED) - Work on trying to get started on the solution before the code is revealed even if you do not know the full answer. This trial and error process will help you become a better programmer. - Add a comment on the first line of the.java file. The comment will be your first and last name. 6 7 - Press Enter
RANDOM NUMBER GAME PROJECT (CONTINUED) ALL coding will be done in the main method - Next we create an instance of the Random class and an instance of the Scanner class. 8
- To generate a random number we will use the generator.nextint class and passing it as a number. If we pass in the number 10 as an example, it will give us a random number between 0 and 9. Because of this we need to add one after we create the random number. - Since we are generating a number we will use the integer variable data type with the name randomnumber. 9 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the coding to randomly generate a number between 1 and 100.
RANDOM NUMBER GAME PROJECT (CONTINUED) - Add a variable to keep the while loop running. Lets give this variable a name of "running" and make it an integer. We will set the running variable to a default value of 1. 10
- Now we create the while loop. We want to keep this running as long as the "running" variable remains unchanged. 11 RANDOM NUMBER GAME PROJECT (CONTINUED) - Directly below the running variable, start the while loop.
- Inside the while loop, add a print statement that will print out the following text: Guess a number between 1 and 100: 12 RANDOM NUMBER GAME PROJECT (CONTINUED) 13 - Use input.nextint(); to retrieve the user's input and store it in and integer variable called guess. Add this code below the print statement. int guess = input.nextint();
- Now we will create a nested if statement similiar to the one we used in the Gradebook project. - The first part of the if statement refers to the users guess that is below the random number generated and will say: "If guess is less than randomnumber Then print a line that says "Too Low". 14 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the first part of the if statement to the while loop. (Refer back to the Gradebook porject if you do not remember how to structure an if statement)
- The second part of the if statement refers to the users guess that is above the random number generated and will say: "If guess is greater than randomnumber Then print a line that says "Too High". - This should be below the first if statement as an "else if" statement. 15 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the second part of the if statement to the while loop.
- The third part of the if statement refers to the users guess that is the random number generated and will say: "If guess is equal to randomnumber Then print a line that says "Correct! You win!. - This should be below the first two if statements as an "else if" statement also. The correct way to compare "is equal to" is to use a double equal sign in the if statement (ex: "=="). 16 17 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the second part of the if statement to the while loop. - We will also have to set the running variable to 0 so the program ends. This is put directly below the Correct! You win! print statement. running = 0;
RANDOM NUMBER GAME PROJECT (CONTINUED) - Here is the completed project. - Run the program to check that it works correctly.
RANDOM NUMBER GAME PROJECT (CONTINUED) - When you run the program it will look like this as you try to guess the randomly generated number.
- Now that you have the number game working, lets make one change and add one more item. 18 19 20 RANDOM NUMBER GAME PROJECT (CONTINUED) - First, change the random number to be from 0 to 200. - Add a counter variable that keeps track of how many guesses it takes for you to get the correct number (hint: you used a counter variable in Gradebook.java). - When you get the correct number, have the program print out how many guesses it took. Create a print statement that says It took you # guesses. (the # symbol refers to the number of guesses)
RANDOM NUMBER GAME PROJECT (CONTINUED) - After you have made the changes on slide 15, the program will look like this.
PRINTING 21 - Print the NumberGame.java file and turn in