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...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
C++ Please For this assignment, you will write a program that lets the user play against...
C++ Please For this assignment, you will write a program that lets the user play against the computer in a variation of the popular blackjack car game. In this variation of the game, two-six sided dice are used instead of cards. The dice are rolled, and the player tries to beat the computer's hidden total without going over 21. Here are some suggestions for the game's design: Each round of the game is performed as an iteration of a loop...
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 a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT