kandi background
Explore Kits

Guessing Number Game using Java

by Abdul Rawoof A R Updated: Nov 23, 2022

The number guessing game is based on a concept where player guesses a number between a range. If player guesses the exact number then player wins else player looses the game. Since this game provides limited attempts, so, player must guess the number with the limited attempts, else will lose the game. You must enter only valid integer within the specified range. You will be provided limited attempts to guess the number. If the entered number is less than, greater than or equal to the required number, then player have answer the answer whether the number is greater, lesser or equal to to proceed further.

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

1import java.util.AbstractList;
2import java.util.Collections;
3import java.util.Scanner;
4
5public class Numberguessing {
6  public static void main(String[] args) {
7    System.out.println("Please think for a number between 1 and 1024 inclusive");
8    System.out.println("I will know your number after max 10 questions!");
9    System.out.println("I will guess your number!");
10    System.out.println("After each guess, respond with littler, greater, or equal depending on my guess.");
11    int result = Collections.binarySearch(new AbstractList<Integer>() {
12      private final Scanner in = new Scanner(System.in);
13      public int size() { return 1025; }
14      public Integer get(int i) {
15        System.out.printf("My guess is: %d. Is it littler, greater, or equal? ", 0 + i);
16        String s = in.nextLine();
17        assert s.length() > 0;
18        switch (s.toLowerCase()) {
19          case "greater":
20            return -1;
21          case "littler":
22            return 1;
23          case "equal":
24            return 0;
25        }
26        return -1;
27      }
28    }, 0);
29
30    if (result < 0) {
31      System.out.println("That is impossible.");
32    } else {
33      System.out.printf("Your number is %d.\n", result);
34    }
35  }
36}
37 Please think for a number between 1 and 1024 inclusive
38I will know your number after max 10 questions!
39I will guess your number!
40After each guess, respond with littler, greater, or equal depending on my guess.
41My guess is: 512. Is it littler, greater, or equal?  greater
42My guess is: 768. Is it littler, greater, or equal?  Littler
43My guess is: 640. Is it littler, greater, or equal?  littler
44My guess is: 576. Is it littler, greater, or equal?  littler
45My guess is: 544. Is it littler, greater, or equal?  littler
46My guess is: 528. Is it littler, greater, or equal?  littler
47My guess is: 520. Is it littler, greater, or equal?  littler
48My guess is: 516. Is it littler, greater, or equal?  littler
49My guess is: 514. Is it littler, greater, or equal?  littler
50My guess is: 513. Is it littler, greater, or equal?  EQUAL
51Your 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

Gaming
  • © 2022 Open Weaver Inc.