陷入无限 do while 循环
我正在尝试用 C++ 进行简单的猜测我的数字游戏,但计算机需要猜测我的数字。但问题是我陷入了这个无限循环。我只是一个初学者,所以这是一个非常基本的程序。
这是我的代码:
int secretNumber = rand() %100 + 1; // random number between 1-100
int tries=0;
int input;
cout <<"typ your number\n";
cin >> input;
do
{
cout <<secretNumber <<endl;
++tries;
if (secretNumber > input)
{
cout <<"To high i guess?\n";
}
else if (secretNumber < input)
{
cout <<"To low I guess?\n";
}
else
{
cout <<"Yes, i got it in " <<tries <<" tries!";
}
}while (input != secretNumber);
return 0;
}
I am trying to make a simple guess my number game in c++ but the computer need to guess my number. But the problem is that I am stuck in this infinite loop. I am just a beginner so it's a really basic program.
This is my code:
int secretNumber = rand() %100 + 1; // random number between 1-100
int tries=0;
int input;
cout <<"typ your number\n";
cin >> input;
do
{
cout <<secretNumber <<endl;
++tries;
if (secretNumber > input)
{
cout <<"To high i guess?\n";
}
else if (secretNumber < input)
{
cout <<"To low I guess?\n";
}
else
{
cout <<"Yes, i got it in " <<tries <<" tries!";
}
}while (input != secretNumber);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
地点
cin>>输入
到循环体中place
cin >> input
into loop body我认为你应该将随机数生成移到循环内。
I think you should move the random number generation inside the loop.
变量
input
的值在循环中永远不会改变,因此终止条件input != SecretNumber
永远不会满足。您应该在循环内获取输入。所以写
cin >>>在循环开始处输入
。编辑:
如果计算机应该猜测,那么仍然需要在循环中更改
input
的值,这在您的代码中不存在。循环每次都使用input
中的相同值运行。为了让你的计算机做出猜测,你应该遵循一些方案。计算机可能会随机抽取数字 - 您可以通过在循环内移动
secretNumber = rand()%100 + 1
来获得这些数字。但这种方法可能性能不佳,循环可能仍会运行很长时间。这在 @Kaii 的回答中有所体现。更有效的方法是二分搜索。在这种情况下,您应该记录计算机所做的猜测。保留两个变量
high
和low
,它们应分别存储高于和低于input
的猜测值。每当猜测值高于该数字时,请将其存储在high
中,并将任何低于input
的猜测值存储在low
中。然后计算机应该尝试在high
和low
之间进行新的猜测。随机猜测应该是secretNumber = low + rand() % (high - low)
。在最坏的情况下,将需要多达 100 次迭代。为了获得最佳结果,每次猜测应该是(high + low) / 2
。根据条件,每次迭代都会更新high
和low
之一。这种方法将确保计算机在 7 次猜测之内猜出正确的数字。在你的代码中应该是这样的:
The value of the variable
input
is never changed in the loop, so the terminating conditioninput != secretNumber
is never met.You should take the input inside the loop. So write
cin >> input
at the beginning of the loop.Edit:
If the computer should guess, then still the value of
input
needs to be changed in the loop, which is not present in your code. The loop runs with the same value ininput
every time.To make you computer make a guess, you should follow some scheme. The computer may draw the numbers at random - which you can get through moving
secretNumber = rand()%100 + 1
inside the loop. But this approach may not perform good, the loop may still run for a very long time. This is shown in @Kaii's answer.A more efficient approach is the Binary Search. In this case you should keep track of the guesses the computer makes. Keep two variables
high
andlow
which should store the guesses higher and lower thaninput
respectively. Whenever a guess in higher than the number, store it inhigh
, and store any guess lower thaninput
inlow
. Then the computer should try its new guess betweenhigh
andlow
. A random guess should besecretNumber = low + rand() % (high - low)
. In worst case it will take as much as 100 iterations. For the best results, each guess should be(high + low) / 2
. According to the conditions, one ofhigh
andlow
will be updated in each iteration. This approach will ensure that the computer will guess the correct number within 7 guesses.In your code it should be like this:
计算机仅在程序启动时(随机)猜测一次,但每次迭代循环时都应该猜测。您应该在循环内移动随机数生成:
the computer is only guessing (by random) once when the program starts, but should guess each time the loop is iterated. you should move the random number generation inside the loop:
您必须在循环内进行输入。现在,您在开始循环之前提示输入一个值,然后永远不要向用户询问另一个数字。如果无法改变猜测,则循环永远不会退出。
更改为
将解决无限循环。
You have to do the input within the loop. Right now you prompt for a value BEFORE you start looping, then never ask the user for another number. If the guess can't be changed, the loop can never exit.
changing to
will solve the infinite loop.
代码中的无限循环,因为 while 的参数始终为 true,直到获得真实数字为止。
你需要添加另一个参数。例如,您希望将最大尝试次数设置为 5 次。那么就会这样,
当你输入错误的号码5次,申请就结束了
infinite loop in your code because the argument for while always true until you get the true number.
you need to add another argument. example you want set maximal tries to 5 times. then it will be like this
when you input wrong number for 5 times, the application will finish