【发布时间】:2020-09-22 12:16:50
【问题描述】:
我在二维数组的帮助下编写了添加两个矩阵的代码。但运行后,它显示错误[[I@6acbcfc0 的结果。如果您知道[[I@6acbcfc0 的含义,请您也描述一下。以下是代码:
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter dimensions: ");
int rows = sc.nextInt();
int cols = sc.nextInt();
int a[][] = new int [rows][cols];
int b[][] = new int [rows][cols];
System.out.println("Enter first matrix: ");
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix: ");
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
b[i][j] = sc.nextInt();
}
}
int c [][] = new int [rows][cols];
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Result is " + c);
for(int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
输出是
Enter dimensions:
2 2
Enter first matrix:
1 2
1 2
Enter second matrix:
1 2
1 2
Result is [[I@6acbcfc0
2 4
2 4
请帮助我删除“[[I@6acbcfc”,如果您在我的代码中发现任何错误,请更正它,以便我能更好地理解它。谢谢。
【问题讨论】:
-
您好,欢迎您。没有必要每隔一个字都强调一下。
-
打印 c 不是错误
-
@ShubhamSrivastava '但是_为什么_它_是_打印_那_之后_结果?'