In: Computer Science
Project 5-3: Guessing Game
Create an application that lets a user guess a number between 1 and 100.
Console
Welcome to the Guess the Number Game
++++++++++++++++++++++++++++++++++++
I'm thinking of a number from 1 to 100.
Try to guess it.
Enter number: 50
You got it in 1 tries.
Great work! You are a mathematical wizard.
Try again? (y/n): y
I'm thinking of a number from 1 to 100.
Try to guess it.
Enter number: 50
Way too high! Guess again.
Enter number: 30
Too high! Guess again.
Enter number: 15
Too low! Guess again.
Enter number: 23
Too high! Guess again.
Enter number: 19
Too low! Guess again.
Enter number: 21
Too high! Guess again.
Enter number: 20
You got it in 7 tries.
Not too bad! You've got some potential.
Try again? (y/n):
Error! This entry is required. Try again.
Try again? (y/n): x
Error! Entry must be 'y' or 'n'. Try again.
Try again? (y/n): n
Bye - Come back soon!
Specifications
If the user's guess is higher than the random number, the application should display, 'Too high!'
If the user's guess is lower than the random number, the application should display, 'Too low!'
If the user's guess is more than 10 higher or 10 lower than the random number, the application should display, 'Way too high!' or 'Way too low!'
The message that's displayed when the user gets the number should vary depending on the 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.
When the user guesses a number, the application should only accept numbers from 1 to 100.
When the user responds to the Try Again? prompt, the application should only accept a value of y or n .nter
If the user enters invalid data, the application should display an appropriate error message and prompt the user again until the user enters valid data.
The code that's used to validate data should be stored in separate methods.
For example:
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
The code that's used to run the application should also be stored in separate methods.
Use the random() method of the java.lang.Math class to generate a random number.
//Java Code
import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { System.out.println("Welcome to the Guess the Number Game\n" + "++++++++++++++++++++++++++++++++++++"); Scanner sc = new Scanner(System.in); char choice=' '; do { int randomNumber =(int)(100.0*Math.random()); int countTry =0,number; boolean win = false; while (!win) { System.out.println("I'm thinking of a number from 1 to 100."); System.out.println("Try to guess it."); number = getIntWithinRange(sc, "Enter number: ", 1, 100); countTry++; if (number < randomNumber && countTry < 10) { System.out.println("Too low! Guess again."); } if (number > randomNumber && countTry < 10) { System.out.println("Too high! Guess again."); } if (number < randomNumber && countTry >= 10) { System.out.println("Way too low! Guess again."); } if (number > randomNumber && countTry >= 10) { System.out.println("Way too high! Guess again."); } if (randomNumber == number) { System.out.println("You got it in " + countTry + " tries."); if (countTry <= 3) System.out.println("Great work! You are a mathematical wizard."); else if (countTry > 3 && countTry <= 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."); win = true; } } choice = getCharWithinRange(sc,"Try again? (y/n): "); if(choice=='n' || choice =='N') System.out.println("Bye - Come back soon!"); }while (choice=='y' || choice=='y'); } public static char getCharWithinRange(Scanner sc, String prompt) { System.out.print(prompt); char ch = sc.next().charAt(0); while (ch!='Y' && ch!='y' && ch!='n' && ch!='N') { if(ch==' ') System.err.println("Error! This entry is required. Try again."); else System.err.println("Error! Entry must be 'y' or 'n'. Try again."); System.out.print(prompt); ch = sc.next().charAt(0); } return ch; } public static int getIntWithinRange(Scanner sc, String prompt,int min, int max) { System.out.print(prompt); int num = sc.nextInt(); while (num<min || num>max) { System.err.println("ERROR!!!! ... invalid number entered"); System.out.print(prompt); num = sc.nextInt(); } return num; } }
//Output
//If you need any help regarding this solution ............ please leave a comment ........ thanks