Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the loop statement 1. Open the Main class. Modify the loop that prompts t

ID: 3889162 • Letter: M

Question

Modify the loop statement

1.       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.

2.       Modify the if/else statement so it uses a break statement to jump out of the loop if the user guesses the correct number.

Add some if/else statements

3.       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.”

4.       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

Add a try/catch statement to get a valid integer

5.       In the Main class, add a try/catch statement so it catches the NumberFormatException that’s 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.

Add an if/else statement to make sure the integer is within a range

6.   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.

//--------------------------------main.java

package murach.games;

import java.util.Scanner;

public class Main {

public static void main(String args[]) {
System.out.println("Welcome to the Number Guessing Game");
System.out.println();

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. ");
} else if (guess > game.getNumber()) {
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!");
}
}

//----------------------numbergame.java

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) ;
guessCount = 1;
}

public int getNumber() {
return number;
}

public int getGuessCount() {
return guessCount;
}
  
public int getUpperLimit() {
return upperLimit;
}
  
public void incrementGuessCount() {
guessCount = guessCount + 1;
}
}

Explanation / Answer

import java.util.*;

public class GuessGame
{
public static void main(String args[])
   {
System.out.println(" Welcome to the Number Guessing Game");
      
System.out.println();
      
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();
  
       //Infinite loop (Loop ends when user guesses correct number)
       while (true)
       {
           System.out.print("Enter your guess: ");
          
           //Try Catch block to handle invalid inputs
           try
           {
               //Reading number
               int guess = Integer.parseInt(sc.nextLine());
              
               //If guess is less than or equal to 0
               if(guess <= 0)
                   System.out.println(" Number should be greater than 0 ");
               else if(guess >= game.getUpperLimit()) //If guess is greater than or equal to upper limit
                   System.out.println(" Number should be less than " + game.getUpperLimit() +" ");
               else
               {
                   //Checking guess
                   if (guess < game.getNumber()) // If guess is less than random number
                       System.out.println("Your guess is too low. ");
                   else if ( (guess - game.getNumber()) > 10) // user’s guess is more than 10 higher than the random number
                       System.out.println("Way too high! ");
                   else if (guess > game.getNumber()) // If guess is greater than random number
                       System.out.println("Your guess is too high. ");
                   else   // Correct Guess
                       break;
                  
                   //Incrementing guess count
                   game.incrementGuessCount();
               }
           }
           catch(NumberFormatException ex)
           {
               //Handling exceptions
               System.out.println(" Invalid Number ");
           }
}
  
       System.out.println("Correct! ");
  
System.out.println("You guessed the correct number in " + game.getGuessCount() + " guesses. ");
  
       //Printing message based on number of guesses
       if(game.getGuessCount() <= 3)
           System.out.println("Great work! You are a mathematical wizard.");
       else if(game.getGuessCount() > 3 && game.getGuessCount() <= 7)
           System.out.println("Not too bad! You've got some potential.");
       else
           System.out.println("What took you so long? Maybe you should take some lessons");
      
       System.out.println(" Bye! ");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote