In: Computer Science
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)
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: