【问题标题】:what is wrong with my calculateSum function in java because it return ZEROjava中我的calculateSum函数有什么问题,因为它返回零
【发布时间】:2014-12-26 13:09:01
【问题描述】:

我正在尝试计算二维数组中对角线之后的元素总和,但问题是总和始终为零,它不会改变答案。

我的代码中的错误在哪里以及如何修复它?

我尝试使用嵌套 for 循环,o 尝试使用一个 for 循环,但在这两种情况下,eanswer 仍然为 0。

这是我的代码:

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);
   }
}

这是输出:

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

【问题讨论】:

  • 您求和的数组很可能全是零,即不是您打印出来的那个。
  • @java dev 显示你的数组
  • 好的,我将编辑我的问题并显示孔代码

标签: java for-loop sum multidimensional-array


【解决方案1】:

问题是,在您的 fill 方法中,您正在创建本地 data 数组,该数组填充了值并作为结果返回,但在您的 main 方法中,您没有将此数组存储在任何地方。而在calculate 中,您使用的是int[][] ma = new int[3][3];,其中填充了0

所以也许把你的代码改成类似

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

无论如何,您的代码看起来很奇怪。看起来你希望你的类存储一些数组,但是你没有在任何地方使用这个数组(除了在 fill 方法中你使用它的 length 属性来填充 data 数组 - 这似乎是错误的,因为 data大小可以不同于matrix)。
也许在fill 方法中,您不应该创建新的data 数组,而是应该填充matrix 数组?同样在这种情况下,您不需要将此数组作为参数传递给构造函数,而只需根据rowcol 值创建新数组。
这样你甚至不需要calculate 方法从外部获取任何数组,只需使用matrix 字段。

换句话说,你的代码看起来更像

// 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`
}

现在您可以简单地使用 new Question2(3,3) 并确保它也将具有正确大小的空数组。

fill 方法的时间。不要创建本地的int[][] data数组,而是使用matrix,所以改变

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

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

最后一件事是calculate 方法。您实际上不需要它来从用户那里获取任何数组,因为您的类中已经有 matrix 数组,因此您可以简单地重用它。因此,不要使用calculate(int[][] num),而是使用calculate(),而不是num,使用matrix

所以现在你的代码看起来像

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

【讨论】:

  • 如果我将主要方法更改为您的解决方案,我将收到 java.lang.NullPointerException 错误
  • @javadev 那是因为在您的fill 方法中,您使用的是data.length 而不是matrix.length,但未设置矩阵(它是null,因此它没有类似的方法或字段长度)。您的代码似乎有点奇怪,因为您实际上并没有在任何地方使用matrix。甚至有这样的领域有什么意义?
  • 对不起,我在没有删除 matrix 的情况下更改了数组的名称,现在它可以返回 sum 的答案,但仍然不是正确的答案。如何解决?
  • 我编辑我的问题并在输出中显示更改后的代码
  • @javadev 您不应该如此大幅度地更改问题中的代码,因为它会使发布的答案看起来像是没有回答您的原始问题。您可以添加带有更新信息的新示例,但永远不要更改整个问题。
【解决方案2】:

您有多个结构问题,但是,回答您的问题,替换此行:

q2.fill();

作者:

ma = q2.fill();

【讨论】:

  • 为什么我必须用 ma = q2.fill() 替换 q2.fill()?在我的主要方法中,我用它没有生成的 ma 填充 q2,因为 make ma = q2.fill()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 2018-05-30
  • 2015-07-21
  • 2012-09-07
  • 1970-01-01
  • 2018-11-10
相关资源
最近更新 更多