The task is to write a Java program in which a user will get K trials to guess a randomly generated number. Below are the rules of the game:
- If the guessed number is bigger than the actual number, the program will respond with the message that the guessed number is higher than the actual number.
- If the guessed number is smaller than the actual number, the program will respond with the message that the guessed number is lower than the actual number.
- If the guessed number is equal to the actual number or if the K trials are exhausted, the program will end with a suitable message.
Approach: Below are the steps:
- The approach is to generate a random number using Math.random() method in Java.
- Now using a loop, take K input from the user and for each input print whether the number is smaller or larger than the actual number.
- If within K trials the user guessed the number correctly, print that the user won.
- Else print that he was not able to guess and then print the actual number.
Below is the implementation of the above approach:
Below is the output of the above program:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number Guessing Game in Python using Binary Search
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Tic-Tac-Toe Game in Java
- Find the winner of the game based on greater number of divisors
- Optimal Strategy for a Game | DP-31
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Minimum Players required to win the game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Minimum possible final health of the last monster in a game
- Find the player who will win the Coin game
- Optimal strategy for a Game with modifications
- Two Balls Reachability Game
- Largest odd divisor Game to check which player wins
- Puzzle | Choose the game of Rolling Dice
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.