Question
Write a correct Java application program that plays a number guessing game with the user. In the starter code that you are given to help
Write a correct Java application program that plays a number guessing game with the user.
In the starter code that you are given to help you begin the project, you will find the following lines of code:
Random generator = args.length == 0 ? new Random() : new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100);
You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program will fail all of the tests.
Your program should begin by choosing a random number between 0 and 99 (inclusive). After executing the above statements, the variable secret will be holding a randomly generated number from 0 to 99.
Next, your program should ask the user to try to guess what number was chosen.
If the user does not guess the number correctly, you should tell the user that the secret number is higher or lower than the one they guessed. At this point you should allow them to guess again.
Repeat this process until the user guesses the number correctly.
Whenever the user does correctly guess the number, you should stop the program and print to the screen the number of tries that it took the user to guess the number. Make sure to use correct grammar when printing your final message to the user (for example "1 guess" or "2 guesses" -- make sure your program doesn't say "1 guesses" or "2 guess").
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics (note that the secret number in this example is 32):
I am thinking of a number from 0-99. Try to guess my number: 50 My number is lower. Guess again: 25 My number is higher. Guess again: 33 My number is lower. Guess again: 31 My number is higher. Guess again: 32 Congratulations! It only took you 5 guesses!
Notes:
While testing your program, (and ONLY while testing your program), you can simply print the secret number to the screen at the start of the program. By doing this, you can properly test your program by knowing the correct answer.
For example after the required lines:
Random generator = args.length == 0 ? new Random() : new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100);
Add a print statement:
System.out.println(secret);
The reason for doing this is so you will know the answer to the problem and therefore will know whether or not the program is correctly telling you "higher" or "lower" when you are making your guesses. Obviously, once you know the program works correctly, you will want to remove this print statement from your program, otherwise the user will always know the answer when playing and therefore it isn't much of a game.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started