陷入无限 do while 循环

发布于 2025-01-06 05:11:59 字数 657 浏览 0 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深爱不及久伴 2025-01-13 05:11:59

地点cin>>输入到循环体中

place cin >> input into loop body

私野 2025-01-13 05:11:59

我认为你应该将随机数生成移到循环内。

I think you should move the random number generation inside the loop.

眼中杀气 2025-01-13 05:11:59

变量 input 的值在循环中永远不会改变,因此终止条件 input != SecretNumber 永远不会满足。

您应该在循环内获取输入。所以写 cin >>>在循环开始处输入

编辑:

如果计算机应该猜测,那么仍然需要在循环中更改input的值,这在您的代码中不存在。循环每次都使用 input 中的相同值运行。

为了让你的计算机做出猜测,你应该遵循一些方案。计算机可能会随机抽取数字 - 您可以通过在循环内移动 secretNumber = rand()%100 + 1 来获得这些数字。但这种方法可能性能不佳,循环可能仍会运行很长时间。这在 @Kaii 的回答中有所体现。

更有效的方法是二分搜索。在这种情况下,您应该记录计算机所做的猜测。保留两个变量 highlow,它们应分别存储高于和低于 input 的猜测值。每当猜测值高于该数字时,请将其存储在 high 中,并将任何低于 input 的猜测值存储在 low 中。然后计算机应该尝试在 highlow 之间进行新的猜测。随机猜测应该是 secretNumber = low + rand() % (high - low)。在最坏的情况下,将需要多达 100 次迭代。为了获得最佳结果,每次猜测应该是(high + low) / 2。根据条件,每次迭代都会更新highlow之一。这种方法将确保计算机在 7 次猜测之内猜出正确的数字。

在你的代码中应该是这样的:

int secretNumber = rand() % 100 + 1; // random number between 1-100
int tries=0;
int input;
int low = 1, high = 100;

cout <<"typ your number\n";
cin >> input;

do
{
    secretNumber = (high + low) / 2;
    cout << secretNumber <<endl;
    ++tries;

    if (secretNumber > input)
    {
        cout << "Too high I guess?\n";
        high = secretNumber;
    }

    else if (secretNumber < input)
    {
        cout << "Too low I guess?\n";
        low = secretNumber;
    }

    else 
    {
        cout << "Yes, i got it in " << tries << " tries!";
    }        
} while (input != secretNumber);

return 0;

The value of the variable input is never changed in the loop, so the terminating condition input != 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 in input 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 and low which should store the guesses higher and lower than input respectively. Whenever a guess in higher than the number, store it in high, and store any guess lower than input in low. Then the computer should try its new guess between high and low. A random guess should be secretNumber = 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 of high and low 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:

int secretNumber = rand() % 100 + 1; // random number between 1-100
int tries=0;
int input;
int low = 1, high = 100;

cout <<"typ your number\n";
cin >> input;

do
{
    secretNumber = (high + low) / 2;
    cout << secretNumber <<endl;
    ++tries;

    if (secretNumber > input)
    {
        cout << "Too high I guess?\n";
        high = secretNumber;
    }

    else if (secretNumber < input)
    {
        cout << "Too low I guess?\n";
        low = secretNumber;
    }

    else 
    {
        cout << "Yes, i got it in " << tries << " tries!";
    }        
} while (input != secretNumber);

return 0;
睡美人的小仙女 2025-01-13 05:11:59

计算机仅在程序启动时(随机)猜测一次,但每次迭代循环时都应该猜测。您应该在循环内移动随机数生成:

int secretNumber = 0;
int tries=0;
int input;

cout <<"typ your number\n";
cin >> input;

do
{
    /* the fix is here */
    secretNumber = rand() %100 + 1; // random number between 1-100
    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;

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:

int secretNumber = 0;
int tries=0;
int input;

cout <<"typ your number\n";
cin >> input;

do
{
    /* the fix is here */
    secretNumber = rand() %100 + 1; // random number between 1-100
    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;
榕城若虚 2025-01-13 05:11:59

您必须在循环内进行输入。现在,您在开始循环之前提示输入一个值,然后永远不要向用户询问另一个数字。如果无法改变猜测,则循环永远不会退出。

更改为

do {
    cin >> input;

将解决无限循环。

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

do {
    cin >> input;

will solve the infinite loop.

浊酒尽余欢 2025-01-13 05:11:59

代码中的无限循环,因为 while 的参数始终为 true,直到获得真实数字为止。

你需要添加另一个参数。例如,您希望将最大尝试次数设置为 5 次。那么就会这样,

do{
    //do your stuff here
}while((input != secretNumber)&&(tries<=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

do{
    //do your stuff here
}while((input != secretNumber)&&(tries<=5))

when you input wrong number for 5 times, the application will finish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文