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;
}