#include <iostream>
using namespace std;
int main()
{
string stu_names[]{ "刘备", "关羽", "张飞" };
string course_names[]{ "语文", "数学", "英语" };
const int ROW = 3;//三行
const int COL = 3;//三列
double scores[ROW][COL];
for(int i = 0; i<ROW; i++)//控制列
for (int j = 0; j < COL; j++)//控制行
{
cout << stu_names[i] << "的" << course_names[j] << "成绩";
cin >> scores[i][j];
}
cout << '\t';
for (int i = 0; i < COL; i++)
{
cout << course_names[i] << '\t';
}
cout << endl;
for (int i = 0; i < ROW; i++)
{ cout << stu_names[i] << '\t';
for (int j = 0; j < COL; j++) {
cout << scores[i][j] << '\t';
}
cout << endl;
}
}
要理解双重for循环的执行顺序,外层for的i=0时,内层for的j会分别等于0,1,2。然后外层for的i才会变成1,接着内层for的j,又经历0,1,2....
scores
是二维数组,其每行对应的是stu_names
里的元素,每列对应的是course_names
里的元素。