谁帮我看下这个程序,很简单,就想求3个学生成绩的总分及平均分,但输入2,结果是正确的,输入3为什么不行啊?
#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,int a,float s): num(n),age(a),score(s){}
float total();
float average();
private:
int num,age,score;
static float sum;
static int count;
};
float Student::total()
{
sum=sum+score;
count++;
return sum;
}
float Student::average()
{
return (sum/count);
}
float Student::sum=0;
int Student::count=0;
int main()
{
Student stu[3]=
{
Student(1001,21,11),
Student(1002,22,12),
Student(1005,28,13),
};
int n;
cout<<"please enter the number of the student: ";
cin>>n;
for(int i=0;i<n;i++)
stu[i].total();
cout<<"the sum of the student's score is: "<<stu[i].total()<<endl;
cout<<"the average score of "<<n<<" students is "<<stu[i].average()<<endl;
return 0;
}
为什么输入2,答案才是正确的,输入3,反而错了。。。