java- how to calculate sum of 2 numbers using two 2D arrays? -
i have write program allows input of 2 numbers in 2 2*3 arrays , displays sum of corresponding numbers. not able understand why , how program implemented using two 2*3 arrays @ lost of how should working. still here have come far:
package lesson1; import java.util.*; class myclass{ public static void main(string[] args) { scanner input= new scanner(system.in); int sum; int array1[][]= new int[2][3]; int array2[][]= new int[2][3]; for(int i=0; i<array1.length; i++){ for(int j=0; j<array1[i].length; j++){ array1[i][j]= input.nextint(); for(int x=0; x<array2.length;x++){ for(int y=0; y<array2.length; y++){ array2[x][y]= input.nextint(); sum= array1[i][j]+ array2[x][y]; system.out.println("the sum "+sum); } } } } } }
i believe code complicated
at first have 2 arrays, array1 , array2. that's fine don't need create 4 "for"s, that.
you can
for (int = 0; < array1.length; i++) { (int y = 0; y < array1[i].length; y++ { array1[i][y]= input.nextint(); array2[i][y]= input.nextint(); sum = sum + array1[i][y] + array2[i][y]; } }
to fill them , sum them.
Comments
Post a Comment