First, sorry, when I dont have the best English, its not my first language..
So, to the problem.. Ive got an exercise, that i have to develope a minigame, in which I have to think for a number between 1 and 1024. The program then have to ask if a number is littler, greater than or equal to the number, the user thinks of. But the computer is only allowed to ask 10 questions. However. I dont know, how to do this and i already did it for the last few days. I also didnt find anything for it in the internet. Here is the Code i have till yet:
package numberguessing;
import java.util.Scanner;
public class Numberguessing {
public static void main(String[] args) {
int av = 0;
int rz;
System.out.println("Please think for a number between 1 and 1024");
// 1 2 3 4 5 6 7 8 9 10
// 2 4 8 16 32 64 128 256 512 1024
System.out.println("I will know your number after max 10 questions");
System.out.println("I will name you a number");
System.out.println("You have to say, if the number is littler, greater than or equal to your number");
rz = a/2 ;
System.out.println("Is your number littler, greater than or equal to " + rz +"?");
Scanner sc = new Scanner(System.in);
String an = sc.next();
do{
av++;
if(an.equalsIgnoreCase("littler")) {
rz = rz / 2 ;
System.out.println("Is your number littler, greater than or equal to " + rz +"?");
an = sc.next();
} else if(an.equalsIgnoreCase("greater")){
rz = (rz + 1024) / 2 ;
System.out.println("Is your number littler, greater than or equal to " + rz +"?");
an = sc.next();
} else if(an.equalsIgnoreCase("equal")) {
} else {
System.out.println("Error: The answer wasnt littler, greater or equal");
}} while(!an.equalsIgnoreCase("equal"));
System.out.println("I won the game after " + av + " tries");
sc.close();
}
}
You could use AbstractList
and Collections
to perhaps make it a little easier to follow:
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);
}
}
}
Try it here!
Example usage where I choose to guess 513:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.