有一个与while函数有关的新问题.听起来很简单,我仍然无法绕过它.
就像我上一个程序一样,这个程序在正确和错误的消息之后意外关闭.我希望在输入数字后循环,这样程序就不会停止.感谢您的帮助,如果有的话.
#include <iostream>
using namespace std;
int main()
{
int X = 0; //setting the first variable
int num; //setting the second
while (X == 0) //this should happen whenever X is equal to 0
{
cout << "Type a number bigger than 3. "; //output
X++; //This should increase X, so that the next while function can happen
}
while (X == 1) //again, since I increased x by one, (0+1=1 obviously) this should happen
{
cin >> num; //standard input
if (num > 3) //if function: if num is bigger than three, then this should happen
{
cout << "Correct! Try again!" <<endl; //output
X--; //Here I'm decreasing x by one, since it was 1 before, now it becomes 0. This should make the "while (X == 0)" part happen again, so that another number bigger than three can be entered
}
if (num <= 3) //if function: if num is lesser than or equal to 3, this should happen
{
cout << "Wrong! Try again!" <<endl; //output
X--; //This is supposed to work like the "X--;" before, repeating the code from "while (X==0)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在它变为0.这应该使"while(X == 0)"部分再次发生
不.虽然循环在程序执行期间的任何时候都不会神奇地生效.您只能在从上面的代码到达时输入while循环.程序通常是从上到下执行的.
如果你想继续循环,你需要围绕整个程序循环.while你现在拥有的那些应该是ifs.
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |