Question

In: Computer Science

Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...

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.

Solutions

Expert Solution

//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


Related Solutions

Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
lets say i make a PYTHON code that lets a user guess a number between 1-1000,...
lets say i make a PYTHON code that lets a user guess a number between 1-1000, every failed attempt you get a hint (go lower or go higher) how can i penalize the user if they dont follow the hints, example ( go higher!... your next pick: a smaller number... 2 you loose $100 for not following hint) 2 and the user has unlimited attempts, until he guesses the number, this is made using random.randint(1,1000) function THE ONLY THING IM...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Create an application that makes the user guess a number. The user must be allowed tries....
Create an application that makes the user guess a number. The user must be allowed tries. You must have a loop, user input (Scanner), and a constructor. (JAVA)
We are going to develop a guessing game where the user has to guess a secret...
We are going to develop a guessing game where the user has to guess a secret number. We will start from a simple task and refine our code step by step. Task is to develop a program that Generates a random integer number in the range of [1-10] as the secret number Asks for user to input an integer in the range of [1-10] Print out the secret number and a user's input. Steps Please download this sample code, using...
Program on Visual Basic, VBA Create an application that lets the user enter a number of...
Program on Visual Basic, VBA Create an application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Time Calculator ( PROGRAM VBA EXCEL) Create a application that lets the user enter a number...
Time Calculator ( PROGRAM VBA EXCEL) Create a application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT