Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the loop statement 1. Open the project named ch08_ex3_GuessingGame. 2. Open the Main class. Modify the loop that prompts the user for a number

Modify the loop statement

1. Open the project named ch08_ex3_GuessingGame.

2. Open the Main class. Modify the loop that prompts the user for a number so that it’s 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 user’s 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 user’s number of guesses. For example:

Number of guesses Message

================= =======

<=3 Great work! You are a mathematical wizard. >3 and <=7 Not too bad! You've got some potential. >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 Number Format Exception that’s thrown by the parse Int 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 that’s 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 that’s greater than or equal to the game’s 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. package murach. games; import java.util.Scanner; public class Main { public static void main(String args[]) { System. out. printing ("Welcome to the Number Guessing Game"); System. out. printing ();

Scanner sc = new Scanner(System.in);
NumberGame game = new NumberGame();
System.out.println("I have selected a number between 0 and " +
game.getUpperLimit());
System.out.println();


System.out.print("Enter your guess: "); int guess = Integer.parseInt(sc.nextLine()); while (guess != game.getNumber()) { if (guess < game.getNumber()) { System.out.println("Your guess is too low.\n"); } else if (guess > game.getNumber()) { System.out.println("Your guess is too high.\n"); }
game.incrementGuessCount(); System.out.print("Enter your guess: "); guess = Integer.parseInt(sc.nextLine()); } System.out.println("Correct!\n");

System.out.println("You guessed the correct number in " + game.getGuessCount() + " guesses.\n"); System.out.println("Bye!"); } }package murach.games;

import java.util.Random;

public class NumberGame { private int upperLimit; private int number; private int guessCount; public NumberGame() { this(50); }

public NumberGame(int upperLimit) { this.upperLimit = upperLimit; Random random = new Random(); number = random.nextInt(upperLimit - 1) + 1; guessCount = 1; }

public int getNumber() { return number; }

public int getGuessCount() { return guessCount; }

public int getUpperLimit() { return upperLimit; }

public void incrementGuessCount() { guessCount = guessCount + 1; } }

Step by Step Solution

3.40 Rating (144 Votes )

There are 3 Steps involved in it

Step: 1

Updated Main class file Changed name of class from Main to GuessingGame for testing purpose Java pro... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Programming

Authors: Joyce Farrell

9th edition

1337397075, 978-1337397070

Students also viewed these Programming questions

Question

Morse test is applicable only for SI engines: True/False andJustify

Answered: 1 week ago