Question

In: Computer Science

Write a Java program that implements the Number Guessing Game: 1. First generate a random number...

Write a Java program that implements the Number Guessing Game:

1. First generate a random number (int) between 0 and 100, call it N

2. Read user input (a guess)

3. check the number, if it's smaller than N, output "The number is larger than that"

4. If the input is larger than N, output "The number is smaller than that"

5. If the input is equal to N, output " You got it!", and exit

6. Repeat until the user guesses the right number.

------- PART 2 : ---------------------

1. Modify the program to limit the number of user guesses to 7 (stop after 7 trials)

------- OPTIONAL : --------------------

2. Modify the program to change the role of the user and program (the user picks a number, and the program tries to guess what it is)

Solutions

Expert Solution

Part 1-

We will generate a random number between 1 to 100 using Math.random().

Now we will take inputs from the user and for each input we will check and print if the number guesses is greater or smaller than the given number.

If the guessed number is equal to the given number then the program will end with message "You got it".

Part 2-

In part 2 we have limited number of trials i.e 7.

Firstly we will generate a random number using Math.random().

Now we use a loop , which runs 7 times and print if the guesses number is greater or smaller then the given number.

If the guessed number is equal to the given number or if loop end without guessing the correct number ,then program will end with suitable message.

Optional part-

In this user will give a number and program will guess the number.

This was the approach for solving the problem.

I am giving you code for solving part 1 ,part 2 and optional part of problem by using a menu driven code which has two options -

1. Unlimited guesses(if user will choose 1 the program will work in the way given in part 1)

2. 7 guesses(if user choose 2 then the program will work in the way given in part 2)

3. If user choose 3 then the user will give the number and program will guess it as given in the optional part

We have completed part 1 , 2 and optional part in single program.

Here is the code-

import java.util.Scanner;
class NumberGame 
{
    static void unlimitedGuess() {
        Scanner ms = new Scanner(System.in);
        int n = (int)(Math.random()*101)+1;
        boolean found = false;
        while (!found)
        {
            System.out.println("Guess the number.");
            int guess = ms.nextInt();
            if (guess == n){
                found = true;
                System.out.println("You got it!");
                break;
            }
            else if (guess > n)
                System.out.println("The number is smaller than that.");
            else
                System.out.println("The number is larger than that.");
        }
        if (found)
            System.out.println("Congratulations!");
        else 
            System.out.println("Bad luck!");
    }
    static void sevenGuesses() {
        Scanner ms = new Scanner(System.in);
        int n = (int)(Math.random()*101)+1;
        int count = 7;
        boolean found = false;
        while (count>0)
        {
            System.out.println("Guess the number. ("+count+" chances remaining)");
            count--;
            int guess = ms.nextInt();
            if (guess == n){
                found = true;
                System.out.println("You got it!");
                break;
            }
            else if (guess > n)
                System.out.println("The number is smaller than that.");
            else
                System.out.println("The number is larger than that.");
        }
        if (found)
            System.out.println("Congratulations!");
        else 
            System.out.println("Bad luck!");
    }
    static void systemGuess() {
        Scanner ms = new Scanner(System.in);
        System.out.println("Write any number and I'll guess");
        int n = ms.nextInt();
        int prevg = -1, prevl = 1000;
        while(true) {
            int a = (int)(Math.random()*101)+1;
            while (a<prevl || a>prevg)
                a = (int)(Math.random()*101)+1;
            if (a > n) {
                System.out.println("I guessed "+a+" which is greater than "+n);
                prevg = a;
            }
            else if (a < n)
            {
                System.out.println("I guessed "+a+" which is lesser than "+n);
                prevl = a;
            }
            else {
                System.out.println("I got this!. "+a);
                break;
            }
        }
    }

    public static void main(String[] args) 
    {
        Scanner ms = new Scanner(System.in);
        System.out.println("Welcome to the guessing game!");
        System.out.println("How you want to play?");
        System.out.println("\t1.)Unlimited Guesses.\n\t2.)7 Guesses\n\t3.)Want me to guess?");
        int a = ms.nextInt();
        if (a == 1)
            unlimitedGuess();
        else if (a == 2)
            sevenGuesses();
        else if (a == 3)
            systemGuess();
    }
}

Sample output of program-


Related Solutions

JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
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...
Using pseudocode design a number guessing game program. The program should generate a random number and...
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
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...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the range of 1 to 20. Each random number is associated with a prize money (from 1 to 10000). Use a Dictionary to store the mapping between the random number and the prize money. Ask user for two distinct numbers, a and b, both from 1 to 20; if a and b are not distinct, or out of range, quit the program Lookup the prize...
Python3  Please add comments Write a program that implements the word guessing game - There is a...
Python3  Please add comments Write a program that implements the word guessing game - There is a secret word - The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess. If the user reveals all the letters in the word before getting too many wrong guesses then they win! Otherwise, they lose. 1 - define secret word 2 - create a revealed letter list that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT