首页 > 代码库 > HW7.1

HW7.1

技术分享

 

 1 import java.util.Scanner; 2  3 public class Solution 4 { 5     public static void main(String[] args) 6     { 7         Scanner input = new Scanner(System.in); 8         double[][] matrix = new double[4][4]; 9         System.out.print("Enter a 4-by-4 matrix row by row: ");10         for(int i = 0; i < 4; i++)11             for(int j = 0; j < 4; j++)12                 matrix[i][j] = input.nextDouble();13 14         input.close();15         System.out.println("Sum of the matrix is " + sumMatrix(matrix));16     }17 18     public static double sumMatrix(double[][] m)19     {20         double sum = 0;21         for(double[] i: m)22             for(double j: i)23                 sum += j;24         return sum;25     }26 }

 

HW7.1