Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; public class GuessNumberApp { private static void displayWelcome(int limit) { System.out.println(Guess the number!); System.out.println(I'm thinking of a number from 1 to +

import java.util.Scanner;

public class GuessNumberApp {

private static void displayWelcome(int limit) {

System.out.println("Guess the number!");

System.out.println("I'm thinking of a number from 1 to " + limit);

System.out.println();

}

public static int getRandomInt(int limit) {

double d = Math.random() * limit; // d is >= 0.0 and < limit

int randomInt = (int) d; // convert double to int

randomInt++; // int is >= 1 and <= limit

return randomInt;

}

public static void main(String[] args) {

final int LIMIT = 10;

displayWelcome(LIMIT);

int number = getRandomInt(LIMIT);

Scanner sc = new Scanner(System.in);

int count = 1;

while (true) {

System.out.print("Your guess: ");

int guess = sc.nextInt();

if (guess < 1 || guess > LIMIT) {

System.out.println("Invalid guess. Try again.");

continue;

}

if (guess < number) {

System.out.println("Too low.");

} else if (guess > number) {

System.out.println("Too high.");

} else {

System.out.println("You guessed it in " +

count + " tries. ");

break;

}

count++;

}

System.out.println("Bye!");

}

}

1. As you test the application, enter an invalid number lide "five" to see what happens when the applicaion crashes.

Validate the number using a try/catch statement

2. Add a try/catch statement that catches the InputMismatchException after the statement in the while loop of the main() method that uses the nextInt() method to get the number entered by the user. For this to work, you'll need to import the InputMismatchExceptin class.

3. Move the statement that uses the nextInt() method inside the try block. For this to work, you'll need to declare the guess variable before the try/catch statement so it can be used outside that statement.

4. Add code to the catch block that displays an error message, discards the invalid entry and any other entries on the line, and uses a continue statement to jump to the beginning of the while loop.

5. Test this enhancement by running the application and entering an invalid number.

Validate the number using data validation

6. Comment out the try/catch statement you just added, and replace it with an if/else statement.

7. Add code to the if block that uses the hasNextInt() method of the Scanner class to see if the value entered by the user is an int. If it is, use the nextInt() method to get that value and store it in the guess variable.

8. Add code to the else block that displays an error message, discards the invalid entry and any other entries on the line, and uses a continue statement to jump to the beginning of the while loop.

9. Test the enhancement by running the application and entering an invalid number.

Discard any extra entries when the first entry is valid

10. Run the applicaiton again and enter two or more valid numbers seperated by a space. Press the Enter key and see what happens.

11. Modify the code so the application only accepts the first number the user enters. Then, test this enhancement.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions