我必须制作一个程序,输入 10 个学生的成绩并显示他们的加权和未加权平均值。我是 C++ 编程的新手,所以我知道的不多,我的教授不喜欢人们使用他没有教过的东西。
这是代码:它显示为学生 1,2 的四项考试成绩是多少,等等。当它说“学生 1 的四项考试成绩是多少”时,我怎样才能做到这一点,那么我会能够输入这些。然后输入到 student2、student3 等?
感谢您的宝贵时间。
#include <iostream>
using namespace std;
const int numberofstudents = 10;
int main()
{
int student;
for(student=1; student<=numberofstudents; student++)
cout << "\nWhat are the four test scores of student number " << student << endl;
return 0;
}
最佳答案
我想你想为每个学生读取四个值,如果是这样,那么理解这段代码:
#include <iostream>
using namespace std;
int main()
{
const int numberofstudents = 10;
double scores[numberofstudents][4];
for(int student=0; student<numberofstudents; student++)
{
cout << "\nWhat are the four test scores of student number " << (student+1) << endl;
int i = 0;
while ( i < 4 )
{
//scores is a two dimentional array
//scores first index is student number (starting with 0)
//and second index is ith score
cin >> scores[student][i]; //read score, one at a time!
i++;
}
}
//process scores...
}
既然这是你的家庭作业,那么你自己完成剩下的工作。祝一切顺利!
关于c++ - C++中for循环后如何输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7296201/