Summing Up A 2D Array

前端 未结 3 869
醉话见心
醉话见心 2021-01-28 05:53

Given my current program, I would like it to calculate the sum of each column and each row once the user has entered all their values. My current code seems to be just doubling

3条回答
  •  梦谈多话
    2021-01-28 06:33

    Welcome to the world of Java. First of let's dissect your "doubling the array" code.

    array2d[row][column] = array2d[row][column] + array2d[row][column];
    

    This line of code is the issue. The loops that you have applied tend to update the values of each of the elements in the matrix. For e.g. Suppose

    array2d[1][2]=2
    

    Hence the code mentioned above does this

    array2d[1][2]= array2d[1][2]+array2d[1][2];
    

    which essentially doubles the value of the array.

    You should try something like this:

    //To print the values of rows
    
        for(int i=0;i
                                                            
提交回复
热议问题