Didart

Sum Matrix Elements

Jan 3rd, 2023
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. package MultidimensionalArrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SumMatrixElements {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] input = scanner.nextLine().split(", ");
  10.        
  11.         int rows = Integer.parseInt(input[0]);
  12.         int cols = Integer.parseInt(input[1]);
  13.        
  14.         String[][] matrix = new String[rows][cols];
  15.        
  16.         int sum = 0;
  17.         for (int row = 0; row < rows; row++) {
  18.             matrix[row] = scanner.nextLine().split(", ");
  19.            
  20.             for (int col = 0; col < matrix[row].length; col++) {
  21.                 sum += Integer.parseInt(matrix[row][col]);
  22.             }
  23.         }
  24.         System.out.println(rows);
  25.         System.out.println(cols);
  26.         System.out.println(sum);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment