如何匹配Java计算机的给定输出猜你的数字游戏;二进制搜索
我一直有很多问题,这是由于很快,我想知道是否有人知道如何解决我的问题。我必须创建一个程序,其中: 您的任务是实现一个基于二进制搜索原理的数字猜测器。在每一步中,计算机将查询间隔减半。当间隔包含一个数字时,它会宣布答案。程序用户选择一个介于1和100之间的数字。然后要求计算机猜测该数字 示例输出如下:如何匹配Java计算机的给定输出猜你的数字游戏;二进制搜索,java,Java,我一直有很多问题,这是由于很快,我想知道是否有人知道如何解决我的问题。我必须创建一个程序,其中: 您的任务是实现一个基于二进制搜索原理的数字猜测器。在每一步中,计算机将查询间隔减半。当间隔包含一个数字时,它会宣布答案。程序用户选择一个介于1和100之间的数字。然后要求计算机猜测该数字 示例输出如下: 您的号码是否大于50?(电脑在问这个问题) 否(用户回答是或否) 你的数字大于25吗? 不 你的数字大于13吗? 不 你的数字大于7吗? 对 你的数字大于10吗? 对 你的数字大于12吗? 对 你的
您的号码是否大于50?(电脑在问这个问题)
否(用户回答是或否)
你的数字大于25吗?
不
你的数字大于13吗?
不
你的数字大于7吗?
对
你的数字大于10吗?
对
你的数字大于12吗?
对
你的号码是13吗?
对
答案是13。(计算机宣布最终答案)
谢谢你玩猜谜游戏。
相比之下,我的示例输出是:
您的号码是否大于50?
不
你的数字大于25吗?
不
你的数字大于13吗?
不
你的数字大于7吗?
对
你的数字大于10吗?
对
你的数字大于11吗?
对
你的数字大于12吗?
对
你的号码是12吗?
对
答案是12。
谢谢你玩猜谜游戏。
根据我所做的编辑进行一些修改
代码如下:
//import statements
import java.util.Scanner;
import java.util.ArrayList;
public class Numbers
{
//constant to initialize the ArrayList
private final int AT_MOST = 100;
//anArrayList of type ArrayList<Integer> which is to hold the values from 1 - 100
private ArrayList<Integer> anArrayList;
/**
* Constructor of the Numbers() class which initializes all of the instance fields
*/
public Numbers()
{
anArrayList = new ArrayList<Integer>();
int i =0;
//while loop to initialize anArrayList with values from 1-100
while(i < AT_MOST)
{
anArrayList.add(i+1);
i++;
}
}
public void search()
{
int low = 0;
int high = anArrayList.size();
int i = 0;
int j = 0;
while(low <= high)
{
int mid = (low + high)/2;
mid = anArrayList.get(mid - 1);
Scanner in = new Scanner(System.in);
System.out.println("Is your number greater than " + mid + "?");
String answer = in.nextLine();
if(answer.equalsIgnoreCase("yes"))
{
low = mid + 1;
}
else if (answer.equalsIgnoreCase("no"))
{
high = mid - 1;
low++;
}
if(low == high+1)
{
Scanner in2 = new Scanner(System.in);
System.out.println("Is your number " + mid + "?");
String finalAnswer = in2.nextLine();
if(finalAnswer.equalsIgnoreCase("yes"))
{
System.out.println(mid + " is the answer.");
System.out.println("Thank you for playing the guessing game.");
low = high + 1;;
}
else
{
System.out.println("Please play again, something went wrong!");
low = high + 1;
}
}
}
}
}
既然你努力解决了这个问题,我就开始重组你的数字课程 我做的第一件事就是除掉ArrayList。您可以像遍历ArrayList一样轻松地对整数进行算术运算 我加了两个整数,距离和方向。每次猜测后,距离都会被减半。在距离减小到零之前,计算机猜测高或低。在这一点上,这个数字介于低和高之间 方向只是告诉我们下一次猜测是需要猜测更低的(-1)还是更高的(+1) 我将高低扫描代码拉入它自己的方法中。一开始它看起来很混乱,但它所做的只是告诉我们是猜测更高(真)还是更低(假)。通过将此代码移到它自己的方法中,我可以集中精力于猜测逻辑 最后,我在处理结束时关闭了扫描仪
//import statements
import java.util.Scanner;
public class Numbers {
// constants to start the game
private final int AT_LEAST = 0;
private final int AT_MOST = 100;
/**
* Constructor of the Numbers() class
*/
public Numbers() {
}
public void search() {
int low = AT_LEAST;
int high = AT_MOST;
int guess = (low + high) / 2;
int distance = guess / 2;
int direction = 1;
System.out.println("Guess a number between " + low + " and " + high
+ ".");
Scanner in = new Scanner(System.in);
do {
boolean greaterThan = getHighLowResponse(in, direction, guess);
if (greaterThan) {
low = guess;
guess += distance;
direction = 1;
} else {
high = guess;
guess -= distance;
direction = -1;
}
distance /= 2;
} while (distance != 0);
for (int i = low; i <= high; i++) {
System.out.println("Is your number " + i + "?");
String finalAnswer = in.nextLine().toLowerCase();
if (finalAnswer.equalsIgnoreCase("yes")) {
System.out.println(i + " is the answer.");
System.out.println("Thank you for playing the guessing game.");
break;
}
}
in.close();
}
private boolean getHighLowResponse(Scanner in, int direction, int guess) {
do {
System.out.println("Is your number " + getDirection(direction)
+ " than " + guess + "?");
String answer = in.nextLine().toLowerCase();
if (direction < 0) {
if (answer.equals("yes"))
return false;
if (answer.equals("no"))
return true;
} else {
if (answer.equals("yes"))
return true;
if (answer.equals("no"))
return false;
}
} while (true);
}
private String getDirection(int direction) {
if (direction < 0) {
return "less";
} else {
return "greater";
}
}
}
//导入语句
导入java.util.Scanner;
公共课号{
//开始游戏的时间
私有最终整数至少等于0;
最大值时的专用最终整数=100;
/**
*Numbers()类的构造函数
*/
公众号码(){
}
公开无效搜索(){
int low=至少_;
int high=最大值;
int guess=(低+高)/2;
int距离=猜测/2;
int方向=1;
System.out.println(“猜一个介于“+low+”和“+high”之间的数字
+ ".");
扫描仪输入=新扫描仪(系统输入);
做{
布尔值大于等于getHighLowResponse(沿、方向、猜测);
如果(大于){
低=猜测;
猜测+=距离;
方向=1;
}否则{
高=猜测;
猜测-=距离;
方向=-1;
}
距离/=2;
}而(距离!=0);
对于(inti=low;i)你的问题到底是什么?是不是从10到11而不是12?(另外,第二个例子中12是正确答案吗?有点模棱两可)你需要更加小心地处理低
、高
,尤其是--中
。所有这些+1
和-1
都表明你试图通过尝试和错误来解决问题。更好的方法可能是后退一步,仔细考虑这些变量代表什么,以及如何解决这些问题对不起,我绝对不擅长反向使用二进制搜索,但问题是,是的,它会转到11而不是12,然后它不会转到13,即使当被问到用户输入时,只是说数字不大于13,这并不一定意味着猜测的数字实际上不是13。我确定键没有o处理if/else(answer.equalsIgnoreCase(“yes”)和(“no”)语句,我必须对其进行一些修改,但我不太确定如何修改。是的,我注意到这一切似乎都是为了得到正确的答案而加在一起的,我只是被卡住了。
//import statements
import java.util.Scanner;
public class Numbers {
// constants to start the game
private final int AT_LEAST = 0;
private final int AT_MOST = 100;
/**
* Constructor of the Numbers() class
*/
public Numbers() {
}
public void search() {
int low = AT_LEAST;
int high = AT_MOST;
int guess = (low + high) / 2;
int distance = guess / 2;
int direction = 1;
System.out.println("Guess a number between " + low + " and " + high
+ ".");
Scanner in = new Scanner(System.in);
do {
boolean greaterThan = getHighLowResponse(in, direction, guess);
if (greaterThan) {
low = guess;
guess += distance;
direction = 1;
} else {
high = guess;
guess -= distance;
direction = -1;
}
distance /= 2;
} while (distance != 0);
for (int i = low; i <= high; i++) {
System.out.println("Is your number " + i + "?");
String finalAnswer = in.nextLine().toLowerCase();
if (finalAnswer.equalsIgnoreCase("yes")) {
System.out.println(i + " is the answer.");
System.out.println("Thank you for playing the guessing game.");
break;
}
}
in.close();
}
private boolean getHighLowResponse(Scanner in, int direction, int guess) {
do {
System.out.println("Is your number " + getDirection(direction)
+ " than " + guess + "?");
String answer = in.nextLine().toLowerCase();
if (direction < 0) {
if (answer.equals("yes"))
return false;
if (answer.equals("no"))
return true;
} else {
if (answer.equals("yes"))
return true;
if (answer.equals("no"))
return false;
}
} while (true);
}
private String getDirection(int direction) {
if (direction < 0) {
return "less";
} else {
return "greater";
}
}
}