technology logo
technology logo

Guessing Number Game using Java

share link

by Abdul Rawoof A R dot icon Updated: Dec 22, 2022

Guide Kit Guide Kit  

Java is a popular and effective programming language frequently used to create various applications, from web and workplace to desktop and mobile. It is renowned for its object-oriented architecture and simplicity, and it has a sizable and vibrant community that offers a plethora of libraries and frameworks for various uses.  


The idea behind the number-guessing game is for the player to guess a number from a range. Assuming the player enters a valid number that falls within the range, if that number is less than, more significant than, or equal to, the player will know whether it is greater, lower, or equal so he 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 correctly predict the number; else, 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.

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