java如何求二维数组的和、最大值和最小值。

public class SecondArray {
    public static void main(String[] args) {

//int a[][]= new int[][]{{121,3,121},{121,1212,1212},{121,1212,1212}};//静态分配数组空间
        int a[][] = new int[3][];
        a[0] = new int[]{91, 100, 62};
        a[1] = new int[]{55, 111, 68};
        a[2] = new int[]{1, 49, 90};//动态分配数组空间
        int sum1 = 0;
        for (int i = 0; i < a.length; i++) {
            int sum = 0;
            for (int j = 0; j < a[i].length; j++) {
                sum += a[i][j];//每一排相加
            }
            System.out.println(sum);
            sum1 += sum;//将每一排的加起来
        }
        System.out.println(sum1);

        int max = 0;
        int min = a[0][0];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length - 1; j++) {
                if (max < a[i][j]) {
                    max = a[i][j];//算出最大值
                }
                if (min > a[i][j]) {
                    min = a[i][j];//算出最小值
                }
            }
        }
        System.out.println(max);
        System.out.println(min);
    }
}

 

你可能感兴趣的:(java)