返回不会破坏if语句(递归)java

时间:2017-07-15 02:43:05

标签: java recursion return

我当前的作业是递归,这就是我在这里没有使用循环的原因。

基本上我想返回数组的最终行列式。我可以得到我正在寻找的答案,但即使我在那里有一个return语句,由于某种原因,程序会继续循环一次。

我的输出如下:

j1: 0
j1: 2
j3: 0
n3: 2
j4: 0
n4: 2
j5: 1
j5: 2
j1: 1
j1: 2
j3: 1
n3: 2
j4: 1
n4: 2
j5: 2
j5: 2
j1: 2
j1: 2
Final Det2: 3
j2: 2
n2: 2
j6: 2
n6: 2
j6: 1
n6: 2
Returned value: 18

这基本上意味着我的j值以某种方式从2变回1?

  public class Lab2 {


  public static void main(String[] args) {
    int[][] inputArray = new int[][]{{2, 3},{5,9}};
    int[][] initialArray = new int[][]{{2, 3},{5,9}};
    int j = 0;
    int n = inputArray.length;
    int finalDet = 0;

    System.out.println("Returned value: " + determinant(inputArray, j, initialArray, finalDet, n));
  }
  //power function works!
  public static int power(int i, int j){
    int power = (int) Math.pow(-1, i+j);
    return power;
  }

  public static int determinant(int[][] inputArray, int j, int[][]initialArray, int finalDet, int n){ 

    inputArray = initialArray;
    int minorLength = n;
    int[][] detArray = new int[n-1][n-1];
    int[][] detStep = new int[n][n];
    int row = 1; //starts row for minor at 1 since top row is always deleted
    int detColumn = 0; //index for columns start at 0
    int inColumn = 0;
System.out.println("j1: "+j);
System.out.println("j1: "+n);  
    if(j == n){
      System.out.println("Final Det2: "+ finalDet);
        System.out.println("j2: "+j);
        System.out.println("n2: "+n);
        return finalDet;
   }

    if(n == 1){
      return finalDet;
    }

    System.out.println("j3: "+j);
    System.out.println("n3: "+n);




      //this part does the det function summation

      if (j<n){
        //this part gets the minor until it is small enough to work with
        if (minorLength>1){ 
        detArray = minor(inputArray, detArray, row, 0, 0, j, minorLength);
        System.out.println("j4: "+j);
        System.out.println("n4: "+n);
        inputArray = detArray;
        minorLength = inputArray[0].length;
        }

        finalDet = finalDet + power(0,j)*inputArray[0][0]*initialArray[0][j];
        j++;

        System.out.println("j5: "+j);
        System.out.println("j5: "+n);

        determinant(inputArray, j, initialArray, finalDet, n);
      }

System.out.println("j6: "+j);
System.out.println("n6: "+n);
return finalDet;

  }


  //minor function works!!!
  //this would be so much easier to do iteratively!!!
  public static int[][] minor(int[][] inputArray, int[][] detArray, int row, int detColumn, int inColumn, int j, int n){ //start row at 1, not 0

    if (row < n){
      if(inColumn < n){

        if (inColumn == j){
          minor(inputArray, detArray, row, detColumn, inColumn+1, j, n);

        }
        if (inColumn != j){
          detArray[row-1][detColumn] = inputArray[row][inColumn];
          minor(inputArray, detArray, row, detColumn+1, inColumn+1, j, n);

            }

      }
        if (inColumn == n){
          minor(inputArray, detArray, row+1, 0, 0, j, n);

          }
        }
/*for (int dispRow = 0; dispRow < detArray.length; dispRow++){
    for (int dispCol = 0; dispCol < detArray.length; dispCol++){
  System.out.print(detArray[dispRow][dispCol]);
}
    System.out.println("");
  }   */
return detArray;
}
}

2 个答案:

答案 0 :(得分:0)

j == 1时你没有回复任何东西。我怀疑问题可能出在哪里。

答案 1 :(得分:0)

显然问题是:

determinant(inputArray, j, initialArray, finalDet, n);

设置它等于返回变量停止额外的循环给了我正确的输出。去图!

finalDet= determinant(inputArray, j, initialArray, finalDet, n);