java - java - 如何使用Java单独对二维数组的行和列求和?
问题描述
这是我正在处理的问题:编写一个程序,读取 3 x 4 矩阵并分别显示每列和每行的总和。
这是示例运行: 逐行输入一个 3×4 矩阵:
1.5 2 3 4
5.5 6 7 8
9.5 1 3 1
Sum of the elements at column 0 is 16.5
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 13.0
Sum of the elements at column 3 is 13.0
Sum of the elements at Row 0 is: 10.5
Sum of the elements at Row 0 is: 26.5
Sum of the elements at Row 0 is: 14.5
这是我想出的代码:
package multidimensionalarrays;
public class MultidimensionalArrays {
public static void main(String[] args) {
double sumOfRow = 0;
double[][] matrix = new double[3][4];
java.util.Scanner input = new java.util.Scanner(System.in); //Scanner
System.out.println("Enter a 3 by 4 matrix row by row: ");
//Prompt user to enter matrix numbers
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
matrix[row][col] = input.nextDouble();
}
}
double[] sumOfCol =new double[matrix[0].length];
for (int i = 0; i < matrix.length; i++) { //row
for (int j = 0; j < matrix[i].length; j++) { //column
sumOfRow += matrix[i][j];
sumOfCol[j] += matrix[i][j];
}
System.out.println("Sum of the elements at row " + row + " is: " + sumOfRow);
}
System.out.println("Sum of the elements at column " + col + " is: " + sumOfCol);
}
}
我的问题是,当它到最后打印列和行的总和时,它无法识别row
orcol
变量。我一直在玩它并切换东西可能已经有几个小时了,但我似乎无法做到这一点,有人可以帮我解决我做错的事情吗?另外我不知道我是否正确地进行了列求和?
解决方案
在您的matrix
中,它是来自代码段的 3×4 矩阵double[][] matrix = new double[3][4];
。第一个索引是行索引,第二个索引是列索引。
请注意,您的double[][]
矩阵是一个数组数组。即,matrix
是一个由三个长度为 4 的数组组成的数组,按照惯例,每个子数组可以看作是排列成一行的对象数组。这称为行主要顺序。
例如,在数组中
0 1 2 4
0 1 3 9
0 1 5 15
它实际上存储为
matrix[0]: [matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3]
所以[0 1 2 4]
matrix[1]: [matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3]
所以[0 1 3 9]
matrix[2]: [matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3]
所以[0 1 5 25]
这是一个更简单的算法,它使用循环来查找每行和每列值的值之和,但需要两次:
/* After the prompt code segment and sumOfCol in the main method */
// Row (major index)
for (int row = 0; row < matrix.length; row++) {
int rowSum = 0;
for (int col = 0; col < matrix[row].length; col++) {
rowSum += matrix[row][col];
}
System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
}
// Column (minor index)
// Assuming the length of each row is the same
for (int col = 0; col < matrix[0].length; col++) {
int colSum = 0;
for (int row = 0; row < matrix.length; row++) {
colSum += matrix[row][col];
}
System.out.println("Sum of the elements at col " + col + " is: " + colSum);
}
输出是
Enter a 3 by 4 matrix row by row:
0 1 2 4
0 1 3 9
0 1 5 15
Sum of the elements at row 0 is: 7
Sum of the elements at row 1 is: 13
Sum of the elements at row 2 is: 21
Sum of the elements at col 0 is: 0
Sum of the elements at col 1 is: 3
Sum of the elements at col 2 is: 10
Sum of the elements at col 3 is: 28
推荐阅读
- dart - 为什么 Dart 隔离中的流在没有接收端口的情况下不起作用
- excel - 将单行(名称、开始日期、结束日期)转换为多行
- selenium - 如何使用 Selenium IDE 获取单击值
- php - 如何使用 PHP 通过 id 在 MySQL 表中获取最新元素
- php - 如何在具有 PERL 模式的 PHP 上计算校验和
- python - 单个单词具有多个类别特征的词嵌入
- c# - Graph-API /v1.0/Teams/id 被阻止
- angular - 如何应用文本装饰:单击角度复选框时的行彻底
- halide - Halide 如何应用区间分析?
- r - R:计算组子集的 RMS