本帖最後由 李泳霖 於 2022-1-27 15:10 編輯
利用二維陣列, 試做一個包含五位同學, 各三組分數的成績表格.
表格的形式如下:
- [code]public class Ch31 {
- public static void main(String[] args) {
- String title[]={"座號","姓名","國文","英文","數學","平均"};
- String name[]={"小叮噹","大雄","宜靜","技安","阿福"};
- int score[][]={{78,89,66},
- {77,67,34},
- {90,98,91},
- {57,59,81},
- {45,66,92}};
- int avg;
- for(int i=0; i<6; i++)
- System.out.print(title[i]+"\t");
- System.out.println();
- System.out.println("===========================================");
- for(int i=0; i<5; i++)
- {
- System.out.print((i+1)+"\t"+name[i]+"\t");
- for(int j=0; j<3; j++)
- {
- System.out.print(score[i][j]+"\t");
- }
- avg=Math.round((float)(score[i][0]+score[i][1]+score[i][2])/3);
- System.out.println(avg);
- }
- }
- }
複製代碼- public class Ch31 {
- public static void main(String[] args) {
- String title[]={"座號","姓名","國文","英文","數學","平均"};
- String name[]={"小叮噹","大雄","宜靜","技安","阿福"};
- int score[][]={{78,89,66,0},
- {77,67,34,0},
- {90,98,91,0},
- {57,59,81,0},
- {45,66,92,0}};
- for(int i=0; i<6; i++)
- System.out.print(title[i]+"\t");
- System.out.println();
- System.out.println("============================================");
- for(int i=0; i<5; i++)
- {
- score[i][3]=Math.round((float)(score[i][0]+score[i][1]+score[i][2])/3);
- System.out.print((i+1)+"\t"+name[i]+"\t");
- for(int j=0; j<4; j++)
- System.out.print(score[i][j]+"\t");
- System.out.println();
- }
- }
- }
複製代碼 [/code] |