Java 如何在带有if\else条件的递归方法中正确放置return语句
我正在编写用if\else控制返回语句的递归代码。然而,当我编译时,我会得到一个“missing return statement”,因为我所有的返回都在条件中。我添加了“假”返回(该方法永远不会到达),但我想知道是否有更聪明的解决方案 例如:Java 如何在带有if\else条件的递归方法中正确放置return语句,java,recursion,return,Java,Recursion,Return,我正在编写用if\else控制返回语句的递归代码。然而,当我编译时,我会得到一个“missing return statement”,因为我所有的返回都在条件中。我添加了“假”返回(该方法永远不会到达),但我想知道是否有更聪明的解决方案 例如: private static boolean compare(int[][] a, int i) { if (i == a.length - 1) { return true; } else {
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'
}
私有静态布尔比较(int[][]a,int i){
如果(i==a.length-1){
返回true;
}
否则{
if(calcLineSum(a[i],0,0)=calcLineSum(a[i+1],0,0)){
比较(a,i+1);
}
否则{
返回false;
}
}
返回false;//我认为如果用返回比较(a,I+1);
替换为返回比较(a,I+1);
和calcLineSum(a,I+1,sum);
替换为返回calcLineSum(a,I+1,sum);
它应该编译
每个if
和else if
都应该用返回值来终止我认为如果你用比较(a,I+1);
替换为返回比较(a,I+1);
和calcLineSum(a,I+1,sum);
它应该编译
每个if
和else if
都应该首先使用返回来终止;正确缩进代码并在所有条件中添加大括号。这将大大节省调试时间
如果其格式正确,则很容易发现错误:
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
}
私有静态布尔比较(int[][]a,int i)
{
如果(i==a.length-1)
{
返回true;
}
其他的
{
if(calcLineSum(a[i],0,0)=calcLineSum(a[i+1],0,0))
{
比较(a,i+1);
}
其他的
{
返回false;
}
}
返回false;首先;正确缩进代码并在所有条件中添加大括号。这将大大节省调试时间
如果其格式正确,则很容易发现错误:
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
}
私有静态布尔比较(int[][]a,int i)
{
如果(i==a.length-1)
{
返回true;
}
其他的
{
if(calcLineSum(a[i],0,0)=calcLineSum(a[i+1],0,0))
{
比较(a,i+1);
}
其他的
{
返回false;
}
}
return false;看起来您误解了代码中的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 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 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
}
我所做的只是删除了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;
sum=sum+a[i];
calcLineSum(a,i+1,sum);
}
private static int calcLineSum (int[]a, int i, int sum)
{
if(i==a.length)
return sum;
calcLineSum(a,i+1,sum+a[i]);
}
您的递归:
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
}
我所做的只是删除了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;
sum=sum+a[i];
calcLineSum(a,i+1,sum);
}
private static int calcLineSum (int[]a, int i, int sum)
{
if(i==a.length)
return sum;
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;
}
尝试以下方法:
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;
}
没有你最后的返回语句(你称之为false的语句);确实存在不返回任何内容的条件
例如,在第一个方法中,如果满足以下条件,则不会返回任何内容:
if (calcLineSum(a[i], 0, 0) == calcLineSum(a[i + 1], 0, 0))
同样,对于第二个方法,else块不返回任何内容
作为一种良好的编码实践,请使用{}around if and else块。不带上一个返回语句(您称之为false的语句);确实存在不返回任何内容的条件
例如,在第一个方法中,如果满足以下条件,则不会返回任何内容:
if (calcLineSum(a[i], 0, 0) == calcLineSum(a[i + 1], 0, 0))
同样,对于第二个方法,else块不返回任何内容
作为一种良好的编码实践,请使用{}around if and else块。这里的问题是,具有非void
返回类型的方法必须保证返回该类型的值
忽略特定代码,比较方法的结构基本上是:
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
}
}
如果有任何方法可以运行而不返回值,编译器将调用它。这里的问题是,具有非void
返回类型的方法必须保证返回该类型的值
忽略特定代码,比较方法的结构基本上是:
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
}
}
如果有任何方法可以运行而不返回值,编译器将调用它。1)格式化代码。2)格式化代码。3)如果代码永远不会到达那里,则抛出异常。是什么让你认为代码永远不会到达这些返回值?它绝对会。return
只会退出该返回值对方法的定义,而不是整个递归堆栈。不仅仅是格式化,而是在else
语句上使用大括号,而不是if
语句,这让我感到畏缩。1)格式化你的代码。2)格式化你的代码。3)如果代码永远不会到达那里,抛出异常。你认为你的代码永远不会到达那些返回值是什么原因绝对会。return
只会退出该方法的一次调用,而不是整个递归堆栈。不仅仅是格式化,而是在else
语句上使用大括号,而不是if
语句,这让我感到畏缩。谢谢你,这是一个非常有用的建议!你能详细说明发生了什么以及发生了什么吗再次调用该方法以生成返回语句有什么区别吗?谢谢!@roony正是这样。一个调用该方法并忽略它所做的事情。另一个返回它。谢谢。这是一个非常有用的建议!请详细说明发生了什么,以及调用该方法有什么区别又来做一个retu