[英]How do i set a variable again in a loop
我是C ++的新手,对变量有疑问
int main() {
int a;
int b;
int c;
int e;
int parafechar;
int loop = 10;
while(loop==10) {
cout<< "Coloque a mensal 1\n";
cin >> a;
cout<< "Coloque a mensal 2\n";
cin >> e;
cout <<"Coloque a nota do cnem\n";
cin >> b;
cout << "Coloque a media dos trabalhos\n";
cin >> c;
if(a>b) {
cout << "A media e : " << a*0.5 + b*0.25 + c*0.25<<endl;
} else {
cout << "A media e : " << e*0.5 + b*0.25 + c*0.25<<endl;
}
cout << "aperte uma tecla para fechar o programa\n";
cin >> parafechar;
}
return 0;
}
在最后一行之后,我希望代码再次运行,并再次设置所有变量,但是程序不断进行下去,我该怎么办? (该程序是葡萄牙语,但它会计算成绩)谢谢您的时间和帮助:)
在最后一行之后,我希望代码再次运行,并再次设置所有变量,但是程序不断运行,我该怎么办?
更改while
检入并更改loop
的值,使其最终满足停止循环的条件。
while(loop > 0) {
// Do your stuff...
// Decrement loop. It will eventually become zero
// and the conditional in the while statement will fail.
--loop;
}
线
while (loop==10)
将始终为真,因为循环的值不会在程序中改变。 我能理解的是,您想循环10次。 因此,在这种情况下,您应该写:
while (loop>0){
// your code of calculating grades
loop--;
}
由于您希望循环执行10次,因此可以执行以下操作:
int loop = 10;
while (loop--) {
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.