我正試圖想出一個反向猜測遊戲。電腦猜測我選擇的數字範圍爲1-100。我有二進制搜索算法,但是當我告訴計算機它首先猜測是太高時,它會給我另一個高估,而不是降低。沒有遵守規則的計算機
import java.util.Random;
import java.util.Scanner;
public class ComputersGuessGame {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random value = new Random();
int computerGuess;
int highValue = 100;
int lowValue = 1;
String myAnswer;
do {
computerGuess = value.nextInt(highValue - lowValue +1)/2;
/*
*Above line should use the binary algorithm so the computer can
*make guesses and not just guess my number by going one number at a time
*/
System.out.println("I'm guessing that your number is " + computerGuess);
myAnswer = in.nextLine();
if (myAnswer.equals("tl")){
highValue = computerGuess + 1;//Too Low Answer
}
else if (myAnswer.equals ("th")){
lowValue = computerGuess - 1;//To High Answer
}
} while (!myAnswer.equals("y")); //Answer is correct
in.close();
System.out.println("Thank you, Good Game.");
}
}//Comptuer keeps making random guesses, but if I say too high, it will guess another high number instead of going low.
的工作解決方案,如果我選擇20和電腦猜90.然後是太高。那麼'lowValue'將是89.這意味着生成的下一個隨機數將在範圍內(1,6)...似乎是正確的 – Idos
您能舉出一個完整的輸入/輸出示例嗎? – Idos
我仍在搜索二進制搜索。索莫妮找到了嗎? – Prashant