题目:
遍历二维数组int a[][]={{23,65,43,68},{45,99,86,80},{76,81,34,45},{88,64,48,25}},再通过循环计算该二维数组的两条对角线之和。
代码:
public class Mathcalculate{
public static void main(String[] args) {
int array_1[][];
array_1=new int[4][4];
array_1[0]=new int[] {23,65,43,68};
array_1[1]=new int[] {45,99,86,80};
array_1[2]=new int[] {76,81,34,45};
array_1[3]=new int[] {88,64,48,25};
System.out.println("现在循环遍历二维数组:");
for (int i=0;i<=3;i++){
for(int j=0;j<=3;j++){
System.out.println(array_1[i][j]);
}
}
System.out.println("现在开始计算二维数组的对角线之和:");
int sum=0;
for(int i =0;i<=3;i++){
sum+=array_1[i][3-i]+array_1[i][i];
}
System.out.println(sum);
}
}