【问题标题】:Using two methods to add rows and columns使用两种方法添加行和列
【发布时间】:2016-09-08 21:48:31
【问题描述】:

请帮忙,我应该使用两种单独的方法(一种用于列,一种用于行)来总结二维数组的每个单独的行和列并将它们打印出来(例如:第 1 行 = ,第 2 行= ,第 1 列 = ,第 2 列 = )。到目前为止,我有两种单独的方法,它们分别只获取第一行和第一列,但我一直坚持如何在不更改返回值的情况下打印出其他行/列。到目前为止,这是我所拥有的:

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));  
        System.out.println("\nSum Col 1 = " + sumCol(mat));
    }

    public static int sumRow(int[][] mat)
    {
        int total = 0;

            for (int column = 0; column < mat[0].length; column++)
            {
                total += mat[0][column];
            }
        return total;
    }

    public static int sumCol(int[][] mat)
    {
        int total = 0;

            for (int row = 0; row < mat[0].length; row++)
            {
                total += mat[row][0];
            }
        return total;
    }
}

【问题讨论】:

  • 你为什么返回一个int而不是一个包含总和的数组?
  • 这些方法应该返回特定列/行的总和,还是所有列/行的总和?

标签: java arrays


【解决方案1】:

为这些方法添加rowcol参数,例如:

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

【讨论】:

  • 很好,我只需要更改行/列号,否则完美。 Muchas Gracias Amigo。
【解决方案2】:

为什么不在两个方法中添加一个参数来指示要求和的行或列的索引?

例如public static int sumRow(int[][] mat, int row)

【讨论】:

  • 啊哈!我忽略了添加参数的主要细节。谢谢你,我会试试的。
【解决方案3】:

从以下位置更改您的方法定义:

public static int sumRow(int[][] mat)

到:

public static int sumRow(int[][] mat, int row)

然后对传递给方法的行求和:

total += mat[row][column];

sumCol() 也是如此。

【讨论】:

    【解决方案4】:

    为每个方法增加一个参数: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));
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 2018-08-18
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多