非终止 while 循环

发布于 2024-11-18 00:56:20 字数 1204 浏览 2 评论 0原文

我只是想知道一些事情。我有以下代码:

#include <iostream>
using namespace std;

int main()
{
    int number, largest, counter = 1;
    while (counter <= 10)
    {
          cout << "Enter a number: ";
          cin >> number;
          if (counter = 1)
          {
               largest = number;
          }
          else if (number > largest)
          {
               largest = number;
              }
          counter++;
    }
    cout << "\n\nThe largest number is: " << largest;
    system("pause");
    return 0;
} 

问题是,它永远不会终止。我确实通过稍微修改代码解决了这个问题,但我想知道为什么会发生这种情况。这是固定代码:

#include <iostream>
using namespace std;

int main()
{
    int number, largest, counter = 1;
    cout << "Enter a number: ";
    cin >> number;
    largest = number;
    while (counter < 10)
    {
      cout << "Enter a number: ";
      cin >> number;
      if (number > largest)
      {
           largest = number;
      }
      counter++;
    }
    cout << "\n\nThe largest number is: " << largest << endl;
    system("pause");
    return 0;
}

似乎在删除 else if 语句后它起作用了。发生了什么?

I was just wondering something. I have the following code:

#include <iostream>
using namespace std;

int main()
{
    int number, largest, counter = 1;
    while (counter <= 10)
    {
          cout << "Enter a number: ";
          cin >> number;
          if (counter = 1)
          {
               largest = number;
          }
          else if (number > largest)
          {
               largest = number;
              }
          counter++;
    }
    cout << "\n\nThe largest number is: " << largest;
    system("pause");
    return 0;
} 

The thing is, it never terminates. I did manage to fix the problem by modifying the code a little, but I was wondering why this happened. Here is the fixed code:

#include <iostream>
using namespace std;

int main()
{
    int number, largest, counter = 1;
    cout << "Enter a number: ";
    cin >> number;
    largest = number;
    while (counter < 10)
    {
      cout << "Enter a number: ";
      cin >> number;
      if (number > largest)
      {
           largest = number;
      }
      counter++;
    }
    cout << "\n\nThe largest number is: " << largest << endl;
    system("pause");
    return 0;
}

It seems that after removing the else if statement it worked. What happened?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

巨坚强 2024-11-25 00:56:20
if (counter = 1) 

否则

if (counter == 1) 

,您将在每次迭代时将计数器重置为 1。

if (counter = 1) 

this should be

if (counter == 1) 

otherwise, you're going to reset your counter to 1 each iteration.

云巢 2024-11-25 00:56:20

if (counter = 1) 重新分配 1 来计数器每个循环,这始终 < 10..

您需要 if (counter == 1)

if (counter = 1) reassigns 1 to counter every loop this being always < 10.

You want if (counter == 1).

雄赳赳气昂昂 2024-11-25 00:56:20

一个常见的错误:

if( counter = 1) // assignment operator

这会在每次迭代时将counter的值设置为1,并且循环永远不会结束。

您应该使用

if( counter == 1) // equality operator
           ^^^^

这正是您的意思

A common mistake:

if( counter = 1) // assignment operator

This will set counter's value to 1 at each iteration, and the loop never finishes.

You should use

if( counter == 1) // equality operator
           ^^^^

which is exactly what you mean

怪异←思 2024-11-25 00:56:20
  if (counter = 1)

这不会比较 counter1,而是将 1 分配给 counter,然后检查 counter - 我们刚刚将其设置为 1,因此它始终为正数并且始终为 <= 10

  if (counter = 1)

This does not compare counter and 1, it assigns 1 to counter and then checks counter- which we just set to 1 so it will always be positive and always be <= 10.

玻璃人 2024-11-25 00:56:20

该行

if (counter = 1)

应该是

if (counter == 1)

因为您想要比较,而不是赋值。

The line

if (counter = 1)

Should be

if (counter == 1)

since you want to compare, not to assign value.

静若繁花 2024-11-25 00:56:20

您的第一个示例使用了

if (counter = 1)

而不是

if (counter == 1)

if 语句,因此 if 语句会在每次迭代期间将计数器重置为 1。

Your first example had

if (counter = 1)

instead of

if (counter == 1)

so the if statement would reset counter to 1 during each iteration.

后eg是否自 2024-11-25 00:56:20

你的问题在这里:

if (counter = 1)

分配而不是比较。以更高的警告级别进行编译。

Your problem is here:

if (counter = 1)

Assignment instead of comparison. Compile with higher warning level.

婴鹅 2024-11-25 00:56:20
if (counter = 1)

因此,counter 值永远为 1。

if (counter = 1)

So, counter value is 1 forever.

情定在深秋 2024-11-25 00:56:20

您将 1 分配给计数器而不是比较它,使用 == 而不是 =

You're assigning 1 to counter rather than comparing it, use == instead of =

一影成城 2024-11-25 00:56:20

正如其他注意事项,这是一个常见错误。您可以通过键入

if( 1 == counter )

而不是

if( counter == 1 )

因为

if( 1 = counter )

无法编译来避免它(如果您犯了错误并忘记了“=”)。

As other notice, this is a common error. Your could avoid it by typing

if( 1 == counter )

instead of

if( counter == 1 )

since

if( 1 = counter )

would not compile (if you made the error and forgot an '=').

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