【问题标题】:Terminating a recursive method in java在java中终止递归方法
【发布时间】:2021-08-20 19:54:48
【问题描述】:

我有一个程序正在运行两个递归方法(这些方法添加了二维数组的行和列)。这两种方法都按预期工作,但是我无法让它们在同一个主要方法体中运行。我相信这是因为 rowsum 从未真正终止,因此 colsum 永远没有机会运行。我该如何解决这个问题?

编辑:我还想补充一点,即使代码运行良好,我也会收到以下错误。有什么解释吗?

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("How many rows?: ");
    int rows = scan.nextInt();
    System.out.println("How many columns?: ");
    int cols = scan.nextInt();
    int[][] array = new int[rows][cols];
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array[0].length; j++) {
            array[i][j] = (int) Math.floor(Math.random() * 101);
        }
    }
    System.out.println(Arrays.deepToString(array));
    rowsum(array, 0);
    colsum(array, 0);
}

public static int rowsum(int array[][], int column) {
    int sum = 0;
    for (int i = 0; i < array.length; i++) {
        sum += array[column][i];
    }
    System.out.print("Sum of row " + column + ": " + sum + " | ");
    if (column >= array[0].length) {
        return 0;
    } else {
        return rowsum(array, column + 1);
    }
}

public static int colsum(int array[][], int row) {
    int sum = 0;
    for (int i = 0; i < array[0].length; i++) {
        sum += array[i][row];
    }
    System.out.print("Sum of column " + row + ": " + sum + " | ");
    if (row >= array.length) {
        return 0;
    } else {
        return colsum(array, row + 1);
    }
}

【问题讨论】:

  • 我不确定您的方法打算如何阅读您的代码。因此,请澄清“他们按预期工作”的意思,也许告诉我们您认为他们“应该做什么”。对我来说,阅读您的代码,他们只是为每个自然数求和。
  • 我在上面说过,这些方法对二维数组的行和列求和。
  • 这些方法不符合预期,否则您将不会得到您所显示的异常。

标签: java recursion multidimensional-array


【解决方案1】:

对于递归方法,您需要一个终止状态。你只有一个回报,这意味着你将无限递归。要终止,您需要一个返回 rowsum 以外的内容的条件(可能涉及列)。

列变得无限大,因此数组上存在数组索引越界错误。你应该有

if (column >= 5) {
    return;
} else {
    return rowsum(...)
}

什么的

【讨论】:

  • 您能否更新问题中的代码,以便我可以看到它的样子?
  • 我认为你需要 -1 所以 if (column >= array.length - 1) 和 colsum 一样
  • 我之前试过-1,但没用。现在只有 0,因为我认为这可能是个问题。
  • 错误还是一样吗?还是没有错误?
  • OHHHHH,谢谢!对不起,我理解错了。非常感谢!
【解决方案2】:

为了获得结果,您的递归方法需要一个停止条件。在这种情况下,结束条件是行或列的结束。当它到达列的行尾时,它应该是所有条目的总和。

我必须说有更简单的方法可以做到这一点。递归不是计算列表总和的最佳方法。将采用一种更具迭代性的方法。

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("How many rows?: ");
    int rows = scan.nextInt();
    System.out.println("How many columns?: ");
    int cols = scan.nextInt();
    int[][] array = new int[rows][cols];
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array[0].length; j++) {
           array[i][j] = (int) Math.floor(Math.random() * 2);
        }
    }
    int sumOfRow = calculateSumOfRow(array, 0, 0, 0);//calculate the first row of the matrix
    int sumOfColumn = calculateSumOfColumn(array, 0, 0, 0);//calculate the first column of the matrix
    System.out.println("sum of row: " + sumOfRow);
    System.out.println("sum of col: " + sumOfColumn);
}

public static int calculateSumOfRow(int[][] integers , int rowToCalculate, int columnIndex, int sum) {
    if(columnIndex < integers[rowToCalculate].length) {
        sum += integers[rowToCalculate][columnIndex];
        return calculateSumOfRow(integers, rowToCalculate, ++columnIndex, sum);
    } else {
        return sum;
    }
}

public static int calculateSumOfColumn(int[][] integers , int columnToCalculate, int rowIndex, int sum) {
    if(rowIndex < integers.length) {
        sum += integers[rowIndex][columnToCalculate];
        return calculateSumOfColumn(integers, columnToCalculate, ++rowIndex, sum);
    } else {
        return sum;
    }
}

【讨论】:

  • 是的,我也这么认为。但是,这是针对递归分配的,所以我别无选择。
  • 那么你需要将当前的总和和索引携带到你的下一次迭代中,并确定你何时到达行或列的末尾,以便返回最终的总和。
猜你喜欢
  • 2013-10-29
  • 2018-07-21
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2012-09-03
相关资源
最近更新 更多