我在一项任务中达到了这一点,我希望得到一些指导。基本上,该程序应该让用户想到一个介于 1-100 之间的数字,然后询问它是高于还是低于 50。然后程序输出该范围的中点,直到答案正确为止。例如,如果输入了“h”,它将询问数字是否为 75,如果响应为“l”,它将询问数字是否为 67,等等。
我想我已经建立了框架,但我真的在努力寻找下一步如何找到中点。任何指导将不胜感激。
import java.util.Scanner;
public class numberguess
{
public static void main(String[] args)
{
String shouldPlayAgain = "y";
String response = "h";
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Guess a number between 1 and 100.");
System.out.print("Is it 50? (h/l/c): ");
response = keyboard.nextLine();
if (response.equals("h"))
{
System.out.println("Is it 75? (h/l/c): ");
}
if (response.equals("l"))
{
System.out.println("Is it 25? (h/l/c): ");
}
System.out.print("Great! Do you want to play again? (y/n): ");
shouldPlayAgain = keyboard.nextLine();
}
while (shouldPlayAgain.equals("y"));
}
}