二維數組練習題


在一個二維數組中存放了三名學生的語文和數學的成績,從鍵盤輸入三名學生的成績存儲到二維數組中,分別求語文和數學的總成績及平均分並輸出。

 

//定義一個三行兩列的整型二維數組intArray
int score[][]=new int[3][2];

//從鍵盤輸入學生成績,要求輸入順序與效果圖一致
for(int i=0;i<score.length;i++){
for(int j=0;j<score[0].length;j++){
if(j==0){
System.out.println("請輸入第"+(i+1)+"個學生的語文成績:");
Scanner sc=new Scanner(System.in);
score[i][j]=sc.nextInt();
}
if(j==1){
System.out.println("請輸入第"+(i+1)+"個學生的數學成績:");
Scanner sc=new Scanner(System.in);
score[i][j]=sc.nextInt();
}

}

}


//求語文的總成績和平均分
int i=0,j=0;
int ywzcj=0,sxzcj=0;
for(i=0;i<score.length;i++){
for(j=0;j<score[1].length;j++){
if(j==0){
ywzcj=ywzcj+score[i][j];
}
if(j==1){
sxzcj=sxzcj+score[i][j];
}
}
}
int ywpjf=ywzcj/3;
int sxpjf=sxzcj/3;
System.out.println("語文總成績為:"+ywzcj);
System.out.println("語文平均分為:"+ywpjf);
System.out.println("數學總成績為:"+sxzcj);
System.out.println("數學平均分為:"+sxpjf);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM