【发布时间】:2021-10-04 16:36:48
【问题描述】:
如何让我的程序不停地重复?我希望它继续要求我为多个学生输入相同的信息。并且在我输入一次后不会停止。 有人可以帮忙吗?我真的很感激所有的帮助谢谢你们。
enter code here
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int midterm=0;
int final=0;
cout << "please enter midterm grade: ";
cin >> midterm;
while (midterm < 0 || midterm > 200)
{
cout << "Please enter valid amount must be 0 or greater or 200 or less. please enter grade: ";
cin >> midterm;
}
cout << "please enter final grade: ";
cin >> final;
while (final < 0 || final > 200)
{
cout << "Please enter valid amount must be 0 or greater or 200 or less. Please enter grade";
cin >> final;
}
int total;
total = final + midterm;
if (total > 360 && total <= 400)
{
cout << "your letter grade is A";
}
else if (total > 320 && total <= 360)
{
cout << "your letter grade is B";
}
else if (total > 280 && total <= 320)
{
cout << "your letter grade is C";
}
else if (total > 240 && total <= 280)
{
cout << "your letter grade is D";
}
else if (total <= 240)
{
cout << "your letter is F";
}
}
【问题讨论】:
-
围绕你已有的所有代码添加一个循环?
-
如果您将
main函数中的所有代码提取出来,并将其放入您调用的单独函数中,也会变得容易得多。围绕该单个调用添加循环可能对您来说更容易。
标签: c++ while-loop