我正试图想出一个反向猜测游戏。电脑猜测我选择的数字范围为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