首先,抱歉,当我没有最好的英语时,它不是我的第一语言..
所以,对于这个问题..我有一个练习,我必须开发一个小游戏,我必须考虑1到1024之间的数字。程序然后要问一个数字是否更小,更大用户认为的数量大于或等于数量。但计算机只允许提出10个问题。然而。我不知道,怎么做这个,我已经在最近几天做了。我也没有在互联网上找到任何东西。 这是我的代码:
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();
}
}
答案 0 :(得分:0)
您可以使用AbstractList
和Collections
来让它更容易理解:
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);
}
}
}
试试here!
我选择猜测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.