java中的calculateSum函数有什么问题,因为它返回零
我试图计算2D数组中对角线之后的元素和,但问题是,和总是等于零,这不会改变答案 我的代码中的错误在哪里?如何修复 我尝试对循环使用嵌套,o尝试对循环使用一个,但在这两种情况下,仍然是0 这是我的代码:java中的calculateSum函数有什么问题,因为它返回零,java,for-loop,sum,multidimensional-array,Java,For Loop,Sum,Multidimensional Array,我试图计算2D数组中对角线之后的元素和,但问题是,和总是等于零,这不会改变答案 我的代码中的错误在哪里?如何修复 我尝试对循环使用嵌套,o尝试对循环使用一个,但在这两种情况下,仍然是0 这是我的代码: package test8; import java.util.Scanner; public class Question2 { private int row = 4; private int col = 4; private int[][] matrix;
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
您有多重结构问题,但在回答您的问题时,请替换此行:
q2.fill();
作者:
问题是,在
fill
方法中,您正在创建本地数据
数组,该数组由值填充并作为结果返回,但在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
属性来填充数据
数组-这似乎是错误的,因为数据
的大小可能不同于矩阵
)。
也许在fill
方法中,您不应该创建新的数据
数组,而是应该填充矩阵
数组?同样,在这种情况下,您不需要在构造函数中将此数组作为参数传递,只需基于行
和列
值创建新数组即可。
这样,您甚至不需要使用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`
}
现在您可以简单地使用新问题2(3,3)
,并确保它也将具有正确大小的空数组
填充方法的时间。不要创建本地int[][]数据
数组,只需使用矩阵
即可,因此请更改
data[row][col] = in.nextInt();
到
最后一件事是计算方法。您不需要从用户那里实际获取任何数组,因为类中已经有matrix
array,所以您可以简单地重用它。因此,不要使用calculate(int[][]num)
而是使用calculate()
而不是num
使用matrix
所以现在你的代码看起来像
Question2 q2 = new Question2(3, 3);
q2.fill();
q2.calculate();
很可能您正在求和的数组中满是零,即不是您打印出来的数组。@java dev show your arrayok我将编辑我的问题并显示孔代码为什么我必须用ma=q2.fill()替换q2.fill()?在我的main方法中,我用ma填充q2,但它没有,因为要使ma=q2.fill()如果我将我的main方法更改为您的解决方案,我将得到一个错误java.lang。NullPointerException@javadev这是因为在您的fill
方法中,您使用的是matrix.length
,而不是data.length
,但未设置矩阵(它是null
,因此没有像length这样的方法或字段)。您的代码似乎有点奇怪,因为您实际上没有在任何地方使用matrix
。甚至有这样的字段有什么意义?很抱歉,我更改了数组的名称,但没有删除matrix。现在它可以工作了。它返回了求和的答案,但仍然不是正确的答案如何修复?我编辑了我的问题并显示了更改使用output@javadev您不应该如此剧烈地更改问题中的代码,因为这样会使发布的答案看起来好像没有回答您的原始问题。您可以添加新的示例,其中包含更新的信息,但决不能更改整个问题。
matrix[row][col] = in.nextInt();
Question2 q2 = new Question2(3, 3);
q2.fill();
q2.calculate();