guess the number in java

logistic_guy

Full Member
Joined
Apr 17, 2024
Messages
764
Write an application that plays “guess the number” as follows: Your program chooses the number to be guessed by selecting a random integer in the range \(\displaystyle 1\) to \(\displaystyle 1000\). The application displays the prompt Guess a number between \(\displaystyle 1\) and \(\displaystyle 1000\). The player inputs a first guess. If the player's guess is incorrect, your program should display Too high. Try again. or Too low. Try again. to help the player “zero in” on the correct answer. The program should prompt the user for the next guess. When the user enters the correct answer, display Congratulations. You guessed the number!, and allow the user to choose whether to play again. [Note: The guessing technique employed in this problem is similar to a binary search, which is discussed in Chapter \(\displaystyle 19\), Searching, Sorting and Big O.]
 
Write an application that plays “guess the number” as follows: Your program chooses the number to be guessed by selecting a random integer in the range \(\displaystyle 1\) to \(\displaystyle 1000\). The application displays the prompt Guess a number between \(\displaystyle 1\) and \(\displaystyle 1000\). The player inputs a first guess. If the player's guess is incorrect, your program should display Too high. Try again. or Too low. Try again. to help the player “zero in” on the correct answer. The program should prompt the user for the next guess. When the user enters the correct answer, display Congratulations. You guessed the number!, and allow the user to choose whether to play again. [Note: The guessing technique employed in this problem is similar to a binary search, which is discussed in Chapter \(\displaystyle 19\), Searching, Sorting and Big O.]
show us your effort/s to solve this problem.
 
At the beginning, we will explicitly choose the random number. Let's assume that the random number is \(\displaystyle 336\).

Here is the first idea of my program:

You will guess a number.
You will enter inside a loop while your guess \(\displaystyle \neq 336\).

If your guess is greater than \(\displaystyle 336\), your guess is High.
Enter a new guess:

Else means your guess is less than \(\displaystyle 336\), then your guess is Low.
Enter a new guess:

If the loop terminates, we know that you guessed the number correctly, so Congratulations!

Here is my code with a screenshot:

import java.util.Scanner;

public class Boor {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number = 336;
System.out.println("Guess my number. It's between 1 and 1000.\n");
System.out.print("Enter a guess: ");
int guess = input.nextInt();
System.out.println();

while (guess != number){
if(guess < number){
System.out.print("Your guess is Low.");
System.out.print("\nEnter a new guess: ");
guess = input.nextInt();
System.out.println();
} else {
System.out.print("Your guess is High.");
System.out.print("\nEnter a new guess: ");
guess = input.nextInt();
System.out.println();
}
}
System.out.println("Congratulation! You guessed the computer number.\n");
}
}

guess.png

In the next post, we will try to figure out a way to let the program generate a random number between \(\displaystyle 1\) and \(\displaystyle 1000\) each time the program runs.

💪🎅
 
Top