lyl890

导航

求一个3*3矩阵对角线元素之和

public static void main(String[] args) {
        int[][] a={{1,2,3,},{4,5,6},{7,8,9}};
        System.out.println(a.length);
        int sum=0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if(i==j || i+j==a.length-1){
                    sum+=a[i][j];
                }

                System.out.print("  "+a[i][j]);
            }
            System.out.println();
        }
        System.out.println(sum);
    }

  

posted on 2022-07-09 15:42  静静书中仙  阅读(35)  评论(0编辑  收藏  举报