请帮助,我应该使用两个单独的方法(一个用于列,一个用于行),以便总结每个单独的行和列的2d数组并打印出来(例如:行1 =,第2行=,第1列=,第2列=)。到目前为止,我有两个单独的方法,只分别给我第一行和第一行,但我仍然坚持如何打印出其他行/列而不改变返回值。这就是我到目前为止所拥有的:
mvn -Dmaven.repo.local=/vagrant/.m2/repository
答案 0 :(得分:1)
为什么不在两个方法中添加一个参数来指示要求和的行或列的索引?
例如public static int sumRow(int[][] mat, int row)
答案 1 :(得分:1)
从以下位置更改方法定义:
cd /home/forge/default
git pull origin dev
composer install --no-interaction --no-dev --prefer-dist
php artisan migrate:refresh --force --seed
xdg-open http://url-to-my-domain.com/page-performs-release-tasks
为:
/home/forge/.forge/provision-4912400.sh: line 7: kde-open: command not found
然后再对传递给方法的行求和:
public static int sumRow(int[][] mat)
同样适用于public static int sumRow(int[][] mat, int row)
。
答案 2 :(得分:1)
为这些方法添加row
和col
参数,例如:
public class FinalSumRowColumn {
public static void main(String[] args) {
int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println("\nSum Row 1 = " + sumRow(mat, 0));
System.out.println("\nSum Col 1 = " + sumCol(mat, 0));
System.out.println("\nSum Row 1 = " + sumRow(mat, 1));
System.out.println("\nSum Col 1 = " + sumCol(mat, 1));
System.out.println("\nSum Row 1 = " + sumRow(mat, 2));
System.out.println("\nSum Col 1 = " + sumCol(mat, 2));
}
public static int sumRow(int[][] mat, int row) {
int total = 0;
for (int column = 0; column < mat[row].length; column++) {
total += mat[row][column];
}
return total;
}
public static int sumCol(int[][] mat, int col) {
int total = 0;
for (int row = 0; row < mat[0].length; row++) {
total += mat[row][col];
}
return total;
}
}
答案 3 :(得分:0)
为每个方法添加一个参数:int index
,例如:
public static int sumRow(int[][] mat, int index)
{
int total = 0;
for (int column = 0; column < mat[index].length; column++)
{
total += mat[index][column];
}
return total;
}
当你打印时:
for (int i = 0; i < mat.length; i++) {
System.out.println("Sum Row " + (i+1) + " = " + sumRow(mat, i));
}