Question

In: Computer Science

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 absolute value of (num – guess). To find the absolute value you need to use the method abs in the Math class: Math.abs(num – guess)

(b) If Diff is 0 then the user guessed the correct number

(c) If Diff is not 0 then use the following logic to help the user guesses the number faster.

(c.1) If diff is greater than or equal 50, the program outputs the message indicating that the guess is very high (if the guess is greater than num) or very low (if guess is less than num)

(c.2) If diff is greater than or equal to 30 and less than 50, the program outputs the message indicating that the guess is high (if guess is greater than num) or low (if guess is less than num).

(c.3) If diff is greater than or equal to 15 and less than 30, the program outputs the message indicating the guess is moderately high (if guess is greater than num) or moderately low (if guess is less than num) (c.4) If diff is greater than 0 and less than 15, the program outputs the message indicating that the guess is somewhat high (if guess is greater than num) or somewhat low (if guess is less than num)

Solutions

Expert Solution

Code For Above Problem:

import java.util.Random;
import java.util.Scanner;

public class NumberGuessing {

        public static void main(String[] args) {

                Random rand = new Random();
                // generating random number between 0-100
                int number = rand.nextInt(100);
                Scanner sc = new Scanner(System.in);
                int guess;
                int count = 0;// to hold number of guesses

                // continue guessing number until some conditions occurred
                while (true) {
                        // Asking user to enter guessed number
                        System.out.print("Enter your guess: ");
                        guess = sc.nextInt();
                        // incrementing guessing count
                        count++;
                        // calculating difference between number and guess number
                        int diff = Math.abs(number - guess);
                        // If diff is 0 then the user guessed the correct number and stop guessing
                        if (diff == 0) {
                                System.out.println("user guessed the correct number");
                                break;
                        }
                        // If diff is greater than or equal 50
                        else if (diff >= 50) {
                                // guess is very high (if the guess is greater than num)
                                if (guess > number) {
                                        System.out.println("guess is very high");
                                }
                                // very low (if guess is less than num)
                                else if (number > guess) {
                                        System.out.println("guess is very Low");
                                }
                        }
                        // If diff is greater than or equal to 30 and less than 50
                        else if (diff >= 30) {
                                // guess is high (if the guess is greater than num)
                                if (guess > number) {
                                        System.out.println("guess is high");
                                }
                                // guess is low (if the guess is lesser than num)
                                else if (number > guess) {
                                        System.out.println("guess is Low");
                                }
                        }
                        // If diff is greater than or equal to 15 and less than 30
                        else if (diff >= 15) {
                                // guess is moderately high (if the guess is greater than num)
                                if (guess > number) {
                                        System.out.println("guess is moderately high");
                                }
                                // guess is moderately low (if the guess is lesser than num)
                                else if (number > guess) {
                                        System.out.println("guess is moderately low");
                                }
                        }
                        // If diff is greater than 0 and less than 15
                        else if (diff > 0) {
                                // guess is somewhat high (if guess is greater than num)
                                if (guess > number) {
                                        System.out.println("guess is somewhat high");
                                }
                                // guess is somewhat low (if guess is lesser than num)
                                else if (number > guess) {
                                        System.out.println("guess is somewhat low");
                                }
                        }

                        // if number of guesses reaches 5 stop guessing
                        if (count == 5) {
                                System.out.println("YOU LOSE");
                                break;
                        }
                }
                sc.close();
        }
}

Sample Run Input/Output Results:

Enter your guess: 67
guess is somewhat low
Enter your guess: 80
guess is somewhat high
Enter your guess: 78
user guessed the correct number

Images Of Code:

Image Of Sample Run Input/Output:


Related Solutions

Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
Write a program with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
Write a game where a user has to guess a number from 1 – 6, inclusive....
Write a game where a user has to guess a number from 1 – 6, inclusive. Your program will generate a random number once (pick a number), and will then prompts the user to guess the number for up to 3 times. If the user enters 3 wrong guesses, the program should be terminated along with the losing message. Once the user has successfully guessed the number, tell the user they win, and tell them how many guesses it took...
Write a Java program that lets the user keep track of their homemade salsa sales. Use...
Write a Java program that lets the user keep track of their homemade salsa sales. Use 5-element arrays to track the following. The salsa names mild, medium, sweet, hot, and zesty. The number of each type sold. The price of each type of salsa. Show gross amount of money made (before tax). Calculate how much the user owes in sales tax (6% of the amount made). Then display the net profit (after subtracting the tax).
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
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 -
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT