非终止 while 循环
我只是想知道一些事情。我有以下代码:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
否则
,您将在每次迭代时将计数器重置为 1。
this should be
otherwise, you're going to reset your counter to 1 each iteration.
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)
.一个常见的错误:
这会在每次迭代时将
counter
的值设置为1
,并且循环永远不会结束。您应该使用
这正是您的意思
A common mistake:
This will set
counter
's value to1
at each iteration, and the loop never finishes.You should use
which is exactly what you mean
这不会比较
counter
和1
,而是将1
分配给counter
,然后检查counter - 我们刚刚将其设置为
1
,因此它始终为正数并且始终为<= 10
。This does not compare
counter
and1
, it assigns1
tocounter
and then checkscounter
- which we just set to1
so it will always be positive and always be<= 10
.该行
应该是
因为您想要比较,而不是赋值。
The line
Should be
since you want to compare, not to assign value.
您的第一个示例使用了
而不是
if 语句,因此 if 语句会在每次迭代期间将计数器重置为 1。
Your first example had
instead of
so the if statement would reset counter to 1 during each iteration.
你的问题在这里:
分配而不是比较。以更高的警告级别进行编译。
Your problem is here:
Assignment instead of comparison. Compile with higher warning level.
因此,
counter
值永远为 1。So,
counter
value is 1 forever.您将 1 分配给计数器而不是比较它,使用 == 而不是 =
You're assigning 1 to counter rather than comparing it, use == instead of =
正如其他注意事项,这是一个常见错误。您可以通过键入
而不是
因为
无法编译来避免它(如果您犯了错误并忘记了“=”)。
As other notice, this is a common error. Your could avoid it by typing
instead of
since
would not compile (if you made the error and forgot an '=').