使用if-Java语句的递归
得到两个使用if-Java语句的递归,java,recursion,Java,Recursion,得到两个inta和b,需要找到包括它们在内的所有数字的和 获取递归方法: 使用两个if语句,如果只使用一个if语句代码工作正常, 否则会导致堆栈溢出错误 public static int getSum(int a, int b) { int result = 0; if (a <= 0 && b <= 0) result = getSum(a + 1, b + 1) + a + b; if (a >= 0 && b
int
a
和b
,需要找到包括它们在内的所有数字的和
获取递归方法:
使用两个if
语句,如果只使用一个if
语句代码工作正常,
否则会导致堆栈溢出错误
public static int getSum(int a, int b) {
int result = 0;
if (a <= 0 && b <= 0) result = getSum(a + 1, b + 1) + a + b;
if (a >= 0 && b >= 0) result = getSum(a - 1, b - 1) + a + b;
return result;
}
publicstaticintgetsum(inta,intb){
int结果=0;
如果(a=0)结果=getSum(a-1,b-1)+a+b;
返回结果;
}
如何修复它?您不需要这些if语句。只需按以下步骤操作:
public static int getSum(int a, int b){
if (b < a) return 0;
if (b == a) return b;
return a + getSum(a + 1, b);
}
publicstaticintgetsum(inta,intb){
如果(b
您不需要这些if语句。只需按以下步骤操作:
public static int getSum(int a, int b){
if (b < a) return 0;
if (b == a) return b;
return a + getSum(a + 1, b);
}
publicstaticintgetsum(inta,intb){
如果(b
假设a是1,b是2
if (a <= 0 && b <= 0) result = getSum(a + 1, b + 1) + a + b;
if (a >= 0 && b >= 0) result = getSum(a - 1, b - 1) + a + b;
所以你调用:a=0,b=2。其中一个选择:
result = getSum(0 + 1, 1 + 1) + a + b;
你回来打电话给1,2
从零开始。导致无休止的递归
因此,您当前的方法存在多个问题:
- 如果条件可能适用,则两者都可以。当使用“=”时,当a或b为0时,如果条件生效,则两者均为
- 更糟的是:如图所示,您的设置允许轻松地向下-向上。。。永远
- 除此之外:缺少正确的“停止递归”。对于某些输入,您的代码总是会再次调用自身
- 这里的猜测:您还缺少角点情况,例如a<0,但b>0
所以,长话短说:你的整个算法都是假的,你需要后退一步,重新思考你在这里做什么 假设a是1,b是2
if (a <= 0 && b <= 0) result = getSum(a + 1, b + 1) + a + b;
if (a >= 0 && b >= 0) result = getSum(a - 1, b - 1) + a + b;
所以你调用:a=0,b=2。其中一个选择:
result = getSum(0 + 1, 1 + 1) + a + b;
你回来打电话给1,2
从零开始。导致无休止的递归
因此,您当前的方法存在多个问题:
- 如果条件可能适用,则两者都可以。当使用“=”时,当a或b为0时,如果条件生效,则两者均为
- 更糟的是:如图所示,您的设置允许轻松地向下-向上。。。永远
- 除此之外:缺少正确的“停止递归”。对于某些输入,您的代码总是会再次调用自身
- 这里的猜测:您还缺少角点情况,例如a<0,但b>0
所以,长话短说:你的整个算法都是假的,你需要后退一步,重新思考你在这里做什么 这不需要递归,但我假设您正在尝试学习递归。 请参见注释以了解解释
public static int getSum( int a, int b ) {
if ( a == b ) { // if both are equal there are no numbers in between - so sum is number itself.
return a;
}
// if a < b then increment a to reach b otherwise increment b to reach a.
// this works even if a or b or both are negative.
// So when you increment a, add 'a' only to current sum and then move forward
// When you increment b, add 'b' only to current sum and then move forward.
return a < b ? a + getSum( a + 1, b ) : b + getSum( a, b + 1 );
}
publicstaticintgetsum(inta,intb){
若(a==b){//若两者相等,则中间并没有数字-所以总和就是数字本身。
返回a;
}
//如果a
这不需要递归,但我假设您正在尝试学习递归。
请参见注释以了解解释
public static int getSum( int a, int b ) {
if ( a == b ) { // if both are equal there are no numbers in between - so sum is number itself.
return a;
}
// if a < b then increment a to reach b otherwise increment b to reach a.
// this works even if a or b or both are negative.
// So when you increment a, add 'a' only to current sum and then move forward
// When you increment b, add 'b' only to current sum and then move forward.
return a < b ? a + getSum( a + 1, b ) : b + getSum( a, b + 1 );
}
publicstaticintgetsum(inta,intb){
若(a==b){//若两者相等,则中间并没有数字-所以总和就是数字本身。
返回a;
}
//如果a
对于'a=1,b=2',结果是1而不是3。对于'a=1,b=2',结果是1而不是3。对于记录:您可能需要添加一个显示故障的示例。这里的问题有点明显,但在写问题时,它们应该始终代表一个真实的答案。以防万一:您刚刚达到投票上限,因此除了接受一个答案之外,您还能够对有用的答案表示感谢;-)请注意:您可能需要添加一个显示失败的示例。这里的问题有点明显,但在写问题时,它们应该始终代表一个真实的答案。以防万一:您刚刚达到投票上限,因此除了接受一个答案之外,您还能够对有用的答案表示感谢;-)getSum(0,1)应返回1,但返回值为2。getSum(0,1)应返回1,但返回值为2。