what is wrong with my calculateSum function in jav

2019-09-21 19:17发布

问题:

i am trying to calculate the sum of elements after the diagonal in 2D array but the problem is that the sum is always equal ZERO it do not change the answer.

where is the mistake in my code and how to fix it ?

i tried to use nested for loop and o tried to use one for loop but in both cases th eanswer still 0.

this is my code:

package test8;

import java.util.Scanner;

public class Question2 {

    private int row = 4;
    private int col = 4;
    private int[][] matrix;

    public Question2(int trow, int tcol) {

        this.row = trow;
        this.col = tcol;
    }

    public Question2(int trow, int tcol, int[][] m) {

        this.row = trow;
        this.col = tcol;
        this.matrix = m;
    }

    public int[][] fill() {
        int[][] data = new int[row][col];
        Scanner in = new Scanner(System.in);
        for (int row = 0; row < data.length; row++) {
            for (int col = 0; col < data[row].length; col++) {
                System.out.println("enter the elementss for the Matrix");
                data[row][col] = in.nextInt();
            }
            System.out.println();
        }

        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < data[row].length; col++) {
                System.out.print(data[row][col] + " ");
            }
            System.out.println();
        }
        return data;
    }

    public int calculate(int[][] num) {

        int sum = 0;

        for (int row = 0; row < num.length; row++) {
            //for (int col = row + 1; col < num[row].length; col++) {
                // if(row == col){
                System.out.println(row);
                sum += num[row][row];
                // }
            //}

        }
         System.out.println("the sum is: " + sum);
        return sum;
    }

        public static void main(String[] args) {

    Question2 q2 = new Question2(3, 3);
    int[][] ma = q2.fill();
    q2.calculate(ma);
   }
}

this is the output:

1 2 3 
4 5 6 
7 8 9 
0
1
2
the sum is: 15

回答1:

Problem is that in your fill method you are creating local data array, which is filled with values and returned as result, but in your main method you are not storing this array anywhere. Instead in calculate you are using int[][] ma = new int[3][3]; which is filled with 0.

So maybe change your code to something like

Question2 q2 = new Question2(3, 3);// you don't even need to pass array here if you 
                                   // plan to generate new one, so consider removing 
                                   // array from argument list in this constructor
int[][] ma = q2.fill();//store array with elements in `ma` reference
q2.calculate(ma);      //now `ma` has elements so we can do some calculations

Anyway your code seems strange. It looks like you want your class to store some array, but you are not using this array anywhere (except in fill method where you are using its length properties to fill data array - which seems wrong since data can have different size than matrix).
Maybe in fill method you shouldn't be creating new data array, but instead you should fill matrix array? Also in that case you don't need to pass this array as argument in constructor, but just create new array based on row and col values.
This way you will not even need calculate method to get any array from outside, just use matrix field.

In other words your code can look more like

// remove entirely this constructor, you will not need it
public Question2(int trow, int tcol, int[][] m) {

    this.row = trow;
    this.col = tcol;
    this.matrix = m;
}

// but use only this one
public Question2(int trow, int tcol) {
    this.row = trow;
    this.col = tcol;
    this.matrix = new int[trow,tcol];// just make sure it will initialize `matrix`
}

Now you can simply use new Question2(3,3) and be sure that it will also have empty array with correct sizes.

Time for fill method. Don't create local int[][] data array, just use matrix instead, so change

data[row][col] = in.nextInt();

to

matrix[row][col] = in.nextInt();

Last thing is calculate method. You don't need it to actually take any array from user, because you already have matrix array inside your class so you can simply reuse it. So instead of calculate(int[][] num) make it calculate() and instead of num use matrix.

So now your code can look like

Question2 q2 = new Question2(3, 3);
q2.fill();
q2.calculate();


回答2:

You have multiple structure problem but, responding your question, replace this line:

q2.fill();

by:

ma = q2.fill();