二維陣列練習(二維陣列+for+if...else)
package com.arraydemo;
import java.util.Scanner;
public class TwoArrayDemo {
public static void main(String[] args) {
/*1、定義一個三行兩列的整型二維陣列intArray
2、從鍵盤輸入學生成績,要求輸入順序與效果圖一致。
3、求語文的總成績和平均分
4、求數學的總成績和平均分*/
Scanner sc=new Scanner(System.in); //接收資料
int[][] intArray=new int[3][2]; //定義int型二維陣列
String[] xueke= {"語文","數學"}; //定義字元型陣列
int x=0,y=0; //定義int型變數x和y,並賦予初值
for(int i=0;i<intArray.length;i++) { //第一層for迴圈
for(int j=0;j<intArray[i].length;j++) { //第二層for迴圈
System.out.println("請輸入第"+(i+1)+"個學生的"+xueke[j]+"成績 :");//輸出第一句語句
intArray[i][j]=sc.nextInt(); //接收的資料進行賦予
if(j==0) { //if條件
x=x+intArray[i][j]; //if判斷
}else { //否則判斷
y=y+intArray[i][j];
}
}
}
System.out.println("語文的總成績為:"+x); //輸出語句
System.out.println("語文的平均成績為:"+(x/3)); //輸出語句
System.out.println("數學的總成績為:"+y); //輸出語句
System.out.println("數學的平均成績為:"+(y/3)); //輸出語句
sc.close(); //結束接收資料
}
}