C++ 找出每个学生的平均分数
有5个学生和3个科目。我的问题是,当计算每个学生的平均分数时,前一个学生的平均分数将被添加到刚刚计算的平均分数中! 任何人都可以找出这段代码中的错误或遗漏!谢谢:)C++ 找出每个学生的平均分数,c++,for-loop,C++,For Loop,有5个学生和3个科目。我的问题是,当计算每个学生的平均分数时,前一个学生的平均分数将被添加到刚刚计算的平均分数中! 任何人都可以找出这段代码中的错误或遗漏!谢谢:) #包括 使用名称空间std; int main() { int i,j; 双平均分,得分,总=0.0; 对于(j=1;j您需要为for循环中的每个学生分配total到0。类似于: for(j=1;j<=5;j++) { cout<<"Marks for Student"<<j<
#包括
使用名称空间std;
int main()
{
int i,j;
双平均分,得分,总=0.0;
对于(j=1;j您需要为for循环中的每个学生分配total
到0
。类似于:
for(j=1;j<=5;j++)
{
cout<<"Marks for Student"<<j<<":"<<endl;
total = 0.0;
for(i=1;i<=3;i++)
{
cout<<" subject"<<i<<":";
cin>>scores;
total+=scores;
}
ave=total/3;
cout<<endl;
}
for(j=1;j在进行下一次平均计算时,您并不是在“清零”总计数器。这是一个很小的问题,如果逐行遍历代码,您就可以解决这个问题
如果您现在不练习,解决更复杂的问题将非常困难。下一个问题,请发布您尝试过的内容。
#include<iostream>
using namespace std;
int main ()
{
int i,j;
double ave,scores,total=0.0;
for(j=1;j<=5;j++)
{
cout<<"Marks for Student"<<j<<":"<<endl;
for(i=1;i<=3;i++)
{
cout<<" subject"<<i<<":";
cin>>scores;
total+=scores;
}
ave=total/3;
// Changes here
cout<<"average:" << ave << endl; // print it here
ave=0; // zero it out
cout<<endl;
}
return 0;
}
#包括
使用名称空间std;
int main()
{
int i,j;
双平均分,得分,总=0.0;
对于(j=1;j),您应该通过单击答案旁边的大复选标记来接受帮助您的答案。
#include<iostream>
using namespace std;
int main ()
{
int i,j;
double ave,scores,total=0.0;
for(j=1;j<=5;j++)
{
cout<<"Marks for Student"<<j<<":"<<endl;
for(i=1;i<=3;i++)
{
cout<<" subject"<<i<<":";
cin>>scores;
total+=scores;
}
ave=total/3;
// Changes here
cout<<"average:" << ave << endl; // print it here
ave=0; // zero it out
cout<<endl;
}
return 0;
}