本帖最後由 鄭繼威 於 2022-12-17 10:52 編輯
針對今天課堂上的練習, 新增 "姓名" 與 "平均" 欄位.
[可複製]- String name[]={"小叮噹","大雄","宜靜","技安","阿福"};
- {{90,85,85},
- {70,75,80},
- {80,95,80},
- {70,95,75},
- {80,85,95}};
複製代碼 法1- public class Ch01{
- public static void main(String args[]){
- int score[][]={{90,95,60},{80,50,40},{99,66,22},{88,77,66},{50,60,70}};
- // 0 1 2 3 4 索引 index
- String name[]={"小叮噹","大雄","宜靜","技安","阿福"};
- System.out.println("座號\t人名\t國文\t英文\t數學\t平均");
- System.out.println("============================");
- float sum;
- for(int i=0;i<=4;i++){
- sum=0;
- System.out.print(i+1+"\t"+name[i]+"\t");
- for(int j=0;j<=2;j++){
- System.out.print(score[i][j]+"\t");
- sum+=score[i][j]; //累加
- }
- System.out.print(sum/3);
- System.out.println();
- }
- }
- }
複製代碼 法2- public class Ch30
- {
- public static void main(String args[])
- { // 0 1 2 3 4 索引 index
- String name[]={"小叮噹","大雄","宜靜","技安","阿福"};
- int score[][]={{90,85,85,0}, //86
- {70,75,80,0}, //75
- {80,95,80,0}, //85
- {70,95,75,0}, //80
- {80,85,95,0}}; //86
- System.out.println("座號\t姓名\t國文\t英文\t數學\t平均");
- System.out.println("==============================================");
- for(int i=0; i<5; i++)
- {
- System.out.print((i+1)+"\t"+name[i]+"\t");
- score[i][3]=(score[i][0]+score[i][1]+score[i][2])/3;
- for(int j=0; j<4; j++)
- System.out.print(score[i][j]+"\t");
- System.out.println();
- }
- }
- }
複製代碼 法3- public class Ch30
- {
- public static void main(String args[])
- { // 0 1 2 3 4 索引 index
- String name[]={"小叮噹","大雄","宜靜","技安","阿福"};
- int score[][]={{90,85,85}, //86
- {70,75,80}, //75
- {80,95,80}, //85
- {70,95,75}, //80
- {80,85,95}}; //86
- System.out.println("座號\t姓名\t國文\t英文\t數學\t平均");
- System.out.println("==============================================");
- for(int i=0; i<5; i++)
- {
- System.out.print((i+1)+"\t"+name[i]+"\t");
- float avg=(float)(score[i][0]+score[i][1]+score[i][2])/3;
- for(int j=0; j<3; j++)
- System.out.print(score[i][j]+"\t");
- System.out.println(avg);
- }
- }
- }
複製代碼 |