繁体   English   中英

如何在具有if \\ else条件的递归方法中正确放置return语句

[英]How to correctly place a return statement in a recursive method with if\else conditions

我正在编写使用if \\ else控制return语句构建的递归代码。 但是,当我编译时,由于所有返回值都在条件中,因此我得到了“丢失的返回语句”。 我正在添加“假”收益(方法永远都不会到达),但我想知道是否有更明智的解决方案。

例如:

    private static boolean compare(int[][] a, int i) {
     if (i == a.length - 1) {
      return true;
     } 
     else {
       if (calcLineSum(a[i], 0, 0) == calcLineSum(a[i + 1], 0, 0)){
         compare(a, i + 1);
       }
       else { 
        return false;
       }
     }
     return false; //<= = 'fake'
    }
    private static int calcLineSum(int[] a, int i, int sum) {
     if (i == a.length)
      return sum;
     else {
      sum = sum + a[i];
      calcLineSum(a, i + 1, sum);
     }
     return sum; //<= = 'fake'
    }

非常感谢您的投入!

我认为如果您替换compare(a,i+1); return compare(a,i+1); calcLineSum(a,i+1,sum); return calcLineSum(a,i+1,sum); 它应该编译。

每个ifelse if应该以return终止。

首先 适当地缩进代码,并为所有条件添加括号。 它将大大节省您的调试时间。

正确格式化后,很容易发现错误:

private static boolean compare (int[][]a, int i)
{
    if(i==a.length-1)
    {
        return true;
    }
    else
    {
        if(calcLineSum(a[i],0,0)==calcLineSum(a[i+1],0,0))
        {
            compare(a,i+1);
        }
        else 
        {
            return false;
        }
    }
    return false; <=='fake' return
}

您可以清楚地看到语句compare(a,i+1); 缺少return 调试愉快!

看来您误会了return 在你的代码中

private static int calcLineSum (int[]a, int i, int sum)
{
    if(i==a.length)
        return sum; // only returns `sum` from this one invocation,
                    // *not* further up the recursive chain
    else {
        sum=sum+a[i];
        calcLineSum(a,i+1,sum);
        // your code is **still going** here; the `return` from
        // the recursive call didn't change anything
        // also note that `sum` hasn't actually changed here
        // sum is passed by value, and changes to it inside the recursive call
        // don't actually make any difference out here
    }
    return sum; // actually used
}

看起来正确的实现是

private static int calcLineSum(int[] a, int i, int sum) {
  if (i == a.length) {
    return sum;
  } else {
    return calcLineSum(a, i+1, sum + a[i]);
  }
}

试试这种方法:

private static boolean compare (int[][]a, int i) {
    boolean result = false;

    if (i == a.length-1) {
        result = true;
    } else {
        if(calcLineSum(a[i],0,0) == calcLineSum(a[i+1],0,0)) {
                result = compare(a,i+1);
        }
    }

    return result;
}

private static int calcLineSum (int[]a, int i, int sum) {
    int result = sum;

    if (i != a.length) {
        result = calcLineSum(a,i+1,sum+a[i]);
    }

    return result;
}

这里的问题是必须保证具有非void返回类型的方法必须返回该类型的值。

忽略特定的代码,您的compare方法的结构基本上是:

if() {
    return
} else {
    if () {
        // you don't return anything here, 
        // so the method might not do what you promised it would,
        // namely return a value
    } else {
        return
    }
}

如果该方法可以通过任何方式运行而不会返回值,则编译器会在该方法上调用您。

您的递归:

private static int calcLineSum (int[]a, int i, int sum)
{
    if(i==a.length)
        return sum;
    else {
             sum=sum+a[i];
             calcLineSum(a,i+1,sum);
         }
    return sum; //<=='fake' return
}

您可以像这样很好地做到:

private static int calcLineSum (int[]a, int i, int sum)
{
    if(i==a.length)
        return sum;
    sum=sum+a[i];
    calcLineSum(a,i+1,sum);
}

我所做的就是删除其他对象 如果不满足第一个条件,则使用else或不使用else ,将执行以下两行:

sum=sum+a[i];
calcLineSum(a,i+1,sum);

它也简化了您的代码。

此外,您可以删除该行

sum=sum+a[i];

像这样写:

private static int calcLineSum (int[]a, int i, int sum)
{
    if(i==a.length)
        return sum;
    calcLineSum(a,i+1,sum+a[i]);
}

没有最后的退货声明(您称其为假的); 确实有条件不返回任何东西;

例如,在您的第一种方法中,如果满足以下条件,则不会返回任何内容:

if (calcLineSum(a[i], 0, 0) == calcLineSum(a[i + 1], 0, 0))

同样,对于第二种方法,else块不返回任何内容。

作为一种良好的编码习惯,请使用{} arround if和else块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM