print sum of matrices in java

0

Hellos ppl, I come with a curiosity, the code below works well, if I have the following arrays:

         int [][] vec1 = {{1,2,3},{4,5,6}};
         int [][] vec2 = {{7,8,9},{10,11,12}};

It gives me as a result:

         [ 8 10 12 14 16 18 ]

So, my question would be this: how could the result be printed on the screen in a similar way to this:

     [ 8 10 12 ], [14 16 18]

ò

     [8 10 12 ]
     [14 16 18]

Thank you very much for your help

public class Prueba2{
public void sumaM (int[][] a, int[][]b){

   if(a.length == b.length){
       int[][] suma = new int[a.length][b[0].length];

       System.out.print("[ ");
       for(int i = 0; i < a.length; i++){
           for(int j=0; j < a[i].length; j++){

               suma [i][j] = a[i][j] + b[i][j];
              System.out.print(suma[i][j]+ " ");
               //System.out.print(suma[j][i] + " ");

           }

        }
        System.out.println("]");

    }else{
       System.out.println("Las matrices no coinciden en tamaño");
    }

} 

}

    
asked by Barly Espinal 01.07.2018 в 04:37
source

1 answer

1

Good morning Barly

simply add a line break when the second for ends, greetings!

public class Prueba2{
public void sumaM (int[][] a, int[][]b){

   if(a.length == b.length){
       int[][] suma = new int[a.length][b[0].length];

       System.out.print("[ ");
       for(int i = 0; i < a.length; i++){
           for(int j=0; j < a[i].length; j++){

               suma [i][j] = a[i][j] + b[i][j];
              System.out.print(suma[i][j]+ " ");

           }
        System.out.println("],[");
        }
        System.out.println("]");

    }else{
       System.out.println("Las matrices no coinciden en tamaño");
    }

} 
}
    
answered by 01.07.2018 в 08:38