Guessing Number Game using Java

share link

by Abdul Rawoof A R dot icon Updated: Feb 5, 2024

technology logo
technology logo

Guide Kit Guide Kit  

Java creates diverse applications, from the net and place of work to computer and mobile. Its object-oriented architecture and simplicity have made it renowned.  


The community is sizable and vibrant. It offers many libraries and frameworks for various uses. The idea behind the number-guessing game is for the player to guess a number from a range. If the player enters a valid number within the range, they will learn if it is greater, lower, or equal. Then, they can proceed further. To win, the player must predict the number within the allotted attempts in this game. A player wins the game if they predict the number; otherwise, they lose. 


Here is an example of how you might implement this game: 

Fig 1: Preview of code snippet in IDE.

Fig 2: Preview of the output that you will get on running this code from your IDE. Example usage where I choose to guess 513.

Code

import java.util.AbstractList;
import java.util.Collections;
import java.util.Scanner;

public class Numberguessing {
  public static void main(String[] args) {
    System.out.println("Please think for a number between 1 and 1024 inclusive");
    System.out.println("I will know your number after max 10 questions!");
    System.out.println("I will guess your number!");
    System.out.println("After each guess, respond with littler, greater, or equal depending on my guess.");
    int result = Collections.binarySearch(new AbstractList<Integer>() {
      private final Scanner in = new Scanner(System.in);
      public int size() { return 1025; }
      public Integer get(int i) {
        System.out.printf("My guess is: %d. Is it littler, greater, or equal? ", 0 + i);
        String s = in.nextLine();
        assert s.length() > 0;
        switch (s.toLowerCase()) {
          case "greater":
            return -1;
          case "littler":
            return 1;
          case "equal":
            return 0;
        }
        return -1;
      }
    }, 0);

    if (result < 0) {
      System.out.println("That is impossible.");
    } else {
      System.out.printf("Your number is %d.\n", result);
    }
  }
}

Please think for a number between 1 and 1024 inclusive
I will know your number after max 10 questions!
I will guess your number!
After each guess, respond with littler, greater, or equal depending on my guess.
My guess is: 512. Is it littler, greater, or equal?  greater
My guess is: 768. Is it littler, greater, or equal?  Littler
My guess is: 640. Is it littler, greater, or equal?  littler
My guess is: 576. Is it littler, greater, or equal?  littler
My guess is: 544. Is it littler, greater, or equal?  littler
My guess is: 528. Is it littler, greater, or equal?  littler
My guess is: 520. Is it littler, greater, or equal?  littler
My guess is: 516. Is it littler, greater, or equal?  littler
My guess is: 514. Is it littler, greater, or equal?  littler
My guess is: 513. Is it littler, greater, or equal?  EQUAL
Your number is 513.

Instructions

Follow the steps carefully to get the output easily:

  1. Copy the code using the "Copy" button above, and paste it in a Java file in your IDE(IntelliJ Preferable).
  2. Run the file to generate the output.


I hope you found this useful.


I found this code snippet by searching for "Guessing Number Game using Java" in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in IntelliJ IDE and Java jdk-'11.0.17'.
  2. The solution is tested on Java 11.0.17.


Using this solution, we are able to develop an guessing number game using java with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to create an guessing number game using Java.

FAQ

1. How do I generate a random number in Java for the guessing game? 

Use java.util.Random or Math.random() to generate a random number within a specified range. 


2. How can I take user input in Java for the guessed number? 

Use the Scanner magnificence to get hold of enter from the user. 


3. What's the best way to compare the user's guess with the generated number? 

Use conditional statements like if and else to check if the guess is too high, too low, or correct. 


4. How can I install many attempts for the user to guess the number? 

Use a loop (such as a for or while loop) to keep trying until you reach the correct guess or a certain limit. 


5. Is there a recommended way to handle invalid input (non-numeric or out-of-range)? 

Use exception handling with try-and-catch blocks. This manages unexpected input and guides the user. 

Support

  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.


See similar Kits and Libraries