输入8个学生的分数,60分及格,统计通过的人数和本班的通过率,如果中间分数有负数则退出输入,并不统计任何数据,提示输入分数有误,重新运行

img

int T = 8;
double sum = 0;
while(T-- != 0)
{
double scope = scanfer.scanf();
if (scope < 0)
{system.out.println("有毛病,重输.");T++;continue;}
sum += scope;
}
system.out.println(sum / 8);

写了个c++的代码,基本原理相同

#include <iostream>
#include<algorithm>
#include <vector>
using namespace std;
int main()
{

    float per;
    vector<float> res;
    cout << "输入8个人的分数:";
    for (int i = 0; i < 8; ++i)
    {
        cin >> per;
        if (per < 0||per>100)
        {
            cout << "error,please restart!" << endl;
            return 0;
        }
        else
        {
            res.push_back(per);
        }    
    }
    float precent;
    float unqualified = 0;
    for (int i = 0; i < res.size(); ++i)
    {

        if (res[i] > 60)
        {
            unqualified++;
        }
    }
    precent = (unqualified / 8)*100;
    cout << "本班人数: " << 8 << "人" << endl;
    cout << "本班通过率:" << precent << "%" << endl;

    return 0;
}

img