1月10日 作业- 遍历二维数组的两张方法及求和

public class lianxi {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根


 int a[][] = {{1,5,8,4,6},{5,8,6,89,54,25,1},{9,81,23,65,24,60}};
      
      int d = 0;
      
      System.out.print("二维数组所有元素有:");
      
      for (int b[] : a )
      {
          for (int c : b)
          { 
             
            System.out.print(c+"  ");
                 d += c;   
          }
    
      }
      System.out.println("各元素和是"+d);
      
      int sum = 0;
      
      for (int i = 0; i < a.length ; i++ )
      {
          for (int j = 0; j < a[i].length;j++)
          {
             sum += a[i][j]; 
          }
      }
      
      System.out.println("数组的和是"+sum);
    }
    
    
    
    

}

1.二维数组所有元素有:1 5 8 4 6 5 8 6 89 54 25 1 9 81 23 65 24 60 各元素和是474
2.数组的和是474

原文地址:https://www.cnblogs.com/yifangtongxing/p/5118925.html