下面的代码应该询问用户他的体重,如果用户输入> 15,它应该执行一个while循环将其增加到50代码编译成功,但无论你输入> 15它都不会执行while循环.
#include <iostream>
using namespace std;
int main()
{
int weight;
cout << "Current weight?" << endl;
cin >> weight;
cout << "Your weight:" << weight << endl;
if (weight > 15) {
while (weight == 50) {
weight = weight + 1;
cout << "Weight:" << weight << endl;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
while(weight==50){
weight = weight +1;
cout <<"Weight:"<<weight<<endl;
}
Run Code Online (Sandbox Code Playgroud)
上面的循环将继续进行,只要weight
等于50
哪个,通过判断你在问题中所写的内容,不是你想要的.
要解决问题,请将循环条件更改为更合适的条件 ; 没有你想保持长循环的weight
是低于 50
?