假設五年級共有8個班: 1班有30人, 2班有31人, 3班有33人, 4班有32人, 5班有31人, 6班有35人, 7班有28人, 8班有29人. 試利用陣列與for迴圈, 列出五年級各班的人數.
 - #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n[8]={30,31,33,32,31,35,28,29};
- for(int i=0; i<8; i++)
- cout<<"5年"<<i+1<<"班"<<n[i]<<"人"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |