プロンプト:プレーヤーは範囲を(最小値と最大値の両方を)選択し、その範囲の数値を考えます(プログラムに数値を入力する必要はありません)。ゲームでは、プレーヤーの番号を体系的に推測するためにバイナリ検索を使用する必要があります。プレーヤーは、ラウンド間でコンピュータに「高すぎる」または「低すぎる」または「正しい」と伝える必要があります。プログラムは、コンピュータが答えを得るまで、または不正行為を検出するまで(または、確かに答えを知っているまで)続けなければなりません。終了する前に、コンピュータは、それがどれくらいの「ラウンド」であったか(何回の推測が行われたか)を述べるべきである。バイナリ検索を使用してユーザ番号を見つけるにはどうすればいいですか?
問題:コンピュータが初めて間違っていると、ユーザーが高すぎたり、低すぎたり宣言した後、私はあなたが使用する必要があります上下の範囲
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」のMaxとMinをもう一度入力するようにユーザーに指示することを意味しますか?彼らは彼らの最初のラウンドを終えた後? – Searching
まだ動作していませんか? – Searching