Question
JAVA In this exercise, youll improve the Number Guessing Game. Modify the loop statement 1. Open the project named ch08_ex3_GuessingGame in the extra_ex_starts directory. 2.
JAVA
In this exercise, youll improve the Number Guessing Game.
Modify the loop statement
1. Open the project named ch08_ex3_GuessingGame in the extra_ex_starts directory.
2. Open the Main class. Modify the loop that prompts the user for a number so that its an
infinite while loop. When you do that, you only need to code the two statements that
get input from the user at the top of the loop.
3. Modify the if/else statement so it uses a break statement to jump out of the
loop if the user guesses the correct number.
4. Run the application to make sure it works correctly.
Add some if/else statements
5. Within the loop, modify the if/else statement so the application says, "Way too high!" if
the users guess is more than 10 higher than the random number. Otherwise, the
application should just say, Your guess is too high.
6. After the loop, add an if/else statement that displays a message that depends on the
users number of guesses. For example:
Number of guesses Message
>3 and
>7: What took you so long? Maybe you should take some lessons
7. Run the application to make sure it works correctly.
Add a try/catch statement to get a valid integer
8. In the Main class, add a try/catch statement so it catches the NumberFormatException
thats thrown by the parseInt method. If this exception is thrown, display a message to
the console that says, Invalid number and jump to the top of the loop. This should
prompt the user to enter the number again.
9. Run the application to make sure it works correctly.
Add an if/else statement to make sure the integer is within a range
10. Add an if/else statement to make sure the user enters a value between the minimum and
maximum values. If the user enters a value thats less than or equal to 0, display a user-
friendly error message and jump to the top of the loop. Conversely,
if the user enters a value thats greater than or equal to the games upper limit, display a user
- friendly error message and jump to the top of the loop.
11. Run the application to make sure it works correctly.
Here is the source code that needs to be modified:
& Main.java x S NumberGame.java x Source History G - - Q 5 2 1 2 2 2 package murach.games; 3D import java.util.Scanner; public class Main { 7 RI public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner (System.in); Number Game game = new Number Game (0: System.out.println("I have selected a number between 0 and " + game.getUpperLimit(0); System.out.println(); " " no 0 0 2 2 2 2 2 2 2 8 N N & N G G - H H M M M M System.out.print("Enter your guess: "); int guess = Integer.parseInt(sc.nextLine()); while (guess != game.getNumber (0) { if (guess game.getNumber (0) { System.out.println("Your guess is too high. "); game.incrementGuessCount(); System.out.print("Enter your guess: "); guess = Integer.parseInt(sc.nextLine()); System.out.println("Correct! "); System.out.println("You guessed the correct number in " + game.getGuessCount() + " guesses. "); System.out.println("Bye!")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