提示:玩家選擇一個範圍(最小和最大),然後考慮該範圍內的數字(不需要在程序中輸入數字)。遊戲應該使用二進制搜索來系統地猜測玩家的號碼。玩家應該在回合之間告訴計算機「太高」或「太低」或「正確」。程序應該繼續,直到電腦得到答案,或檢測到作弊(或肯定知道答案)。在退出之前,計算機應該說出它有多少「輪」(需要多少次猜測)。如何找到使用二進制搜索的用戶號碼
問題:電腦是錯後的第一時間和用戶聲明太高或太低,我不能ressign值的上限和下限範圍
import java.util.Scanner;
public class TestPractice {
public static void main(String[] args) {
System.out.println("Think of a number");
Scanner scan = new Scanner(System.in);
String x = null;
String y = null;
String i = null;
//Get the input from the player
System.out.println("Please your maximum value");
if (scan.hasNext()) {
x = scan.next();
}
System.out.println("Please input your min value");
if (scan.hasNext()) {
y = scan.next();
}
//Parse the input so its usuable in the array
int max = Integer.parseInt(x);
int min = Integer.parseInt(y);
boolean numberguessed = true;
int numberofRounds = 0;
while(numberguessed) {
int midpoint = (max+min)/2;
numberofRounds++;
System.out.println("Is your number " + midpoint + " please say too low or too high or correct");
if (scan.hasNext()) {
i = scan.next();
}
if (i.equalsIgnoreCase("too high")) {
min = midpoint;
}
if (i.equalsIgnoreCase("too low")) {
max = midpoint;
min = 0;
}
if (i.equalsIgnoreCase("correct")) {
System.out.println("the number of rounds in this game is" + numberofRounds);
break;
}
}
}
}
你的意思是你想讓用戶再次輸入'x'和'y'最大值和最小值?他們完成第一輪之後? – Searching
仍然無法使用? – Searching