Java递归不可被特定数整除的数之和

Java递归不可被特定数整除的数之和,java,recursion,sum,Java,Recursion,Sum,我必须用递归法找到所有不可被7整除的偶数之和。我尝试了此代码,但似乎在某个地方出错,因为它返回0: public static void main(String[] args) { System.out.println(specialSum(50)); } public static int specialSum(int a) { if ((a >= 1) && ((specialSum(a-1))%7 !=0)) { return a + sp

我必须用递归法找到所有不可被7整除的偶数之和。我尝试了此代码,但似乎在某个地方出错,因为它返回0:

public static void main(String[] args) {

System.out.println(specialSum(50));
}
public static int specialSum(int a) {

    if ((a >= 1) && ((specialSum(a-1))%7 !=0)) {
        return a + specialSum(a -1);
    } else{
        return 0;
    }

    }
}
如果((a>=1)和((specialSum(a-1))%7!=0))请尝试
if((a>=1)和((a%7)!=0))
,就像现在一样,您永远不会检查原始
a
值是否不能被7整除,您的第一次检查总是
a
-1.

而不是
if((a>=1)和((specialSum(a-1))%7)请尝试
((a>=1)&(a%7)!=0))
,现在,您永远不会检查原始
a
值是否不能被7整除,您的第一个检查总是
a
-1。

您的代码是错误的

条件
(specialSum(a-1))%7=0)
a=50时,在代码中调用
49
的方法。这将调用
48
的方法,该方法调用
47
,依此类推,直到
a=1
。然后,它调用
0
,该值不大于或等于
1
,因此它返回
0
。现在,对于任何数字,
0%n
都是
0
。因此,您得到
0
作为输出

将您的方法更改为以下内容:

public static int specialSum(int a) {
    if(a<=2) return 2; // base case
    else if(a%7==0) return specialSum(a-2); // if it is divisible by 7, then do not add it, and call the method for a-2.
    else return a+specialSum(a-2); // else add the number, and get the answer for a-2.
}
publicstaticintspecialsum(inta){
如果(a您的代码是错误的

条件
(specialSum(a-1))%7!=0)
在代码中,当
a=50
时,调用
49
的方法。这调用
48
的方法,它调用
47
,依此类推,直到
a=1
。然后,它调用
0
,该值不大于或等于
1
,因此它返回
0
。现在,
0%n
对于任何数字,都是
0
。因此,您得到
0
作为输出

将您的方法更改为以下内容:

public static int specialSum(int a) {
    if(a<=2) return 2; // base case
    else if(a%7==0) return specialSum(a-2); // if it is divisible by 7, then do not add it, and call the method for a-2.
    else return a+specialSum(a-2); // else add the number, and get the answer for a-2.
}
publicstaticintspecialsum(inta){

if(a这对我很有效。第一个
if
确保只取偶数。第二个
if
确保不可除7时求和。最后一个
if
求和结果

public static void main(String[] args) {
    System.out.println(specialSum(50, 0));
}

public static int specialSum(int max, int current){
    if(max % 2 == 1)
        max -= 1;
    if(max % 7 != 0)
        current += max;
    if(max >= 1){
        max -= 2;
        return specialSum(max, current);
    }
    return current;
}
返回566。
等于:50+48+46+44+40+38+36+34+32+30+26+24+22+20+18+16+12+10+8+6+4+2。这对我很有效。第一个
if
确保只取偶数。第二个
if
确保只在不可被7整除时求和。最后一个
if
求和结果

public static void main(String[] args) {
    System.out.println(specialSum(50, 0));
}

public static int specialSum(int max, int current){
    if(max % 2 == 1)
        max -= 1;
    if(max % 7 != 0)
        current += max;
    if(max >= 1){
        max -= 2;
        return specialSum(max, current);
    }
    return current;
}
public static int specialSum(int a) {

      if ( a % 7 !=0 && a%2==0 ) {
        return a + specialSum(a - 2);
    } else {
        if (a > 2  ) {
            a=a-2;
            return a + specialSum(a - 2);
        } else {
            return 0;
        }
    }
}
返回566。
等于:50+48+46+44+40+38+36+34+32+30+26+24+22+20+18+16+12+10+8+6+4+2。

在递归中,您只需要关注当前步骤,不应该在某个条件下使用specialSum(a-1)。这是下一步,您应该只在关注当前步骤后调用它

public static int specialSum(int a) {

      if ( a % 7 !=0 && a%2==0 ) {
        return a + specialSum(a - 2);
    } else {
        if (a > 2  ) {
            a=a-2;
            return a + specialSum(a - 2);
        } else {
            return 0;
        }
    }
}
您只需应用两条规则即可成功:仅将当前编号添加到nexts -如果他们是平等的 -如果它们不能被7整除

public static int specialSum(int a) {
    if(a <= 1) // Final Case.  
    {
        System.out.print(" 0 = ");
        return 0;
    }


    if(a%2 != 0) // Even Number, do not sum, call next step
    {
        return specialSum(a-1);
    }
    else 
    { 
        if(a % 7 == 0){ // Divisible by 7  Do not sum, call next step.
            return specialSum(a-1);
        }
        else // NOT divisible by 7 nor by 2, add it to the next step
        {
            System.out.print(a+ " + ");
            return a + specialSum(a-1);
        }

    }

}
publicstaticintspecialsum(inta){

如果(a在递归中,您只需要关注当前步骤,就不应该在条件中使用specialSum(a-1)。这是下一步,您应该只在关注当前步骤后调用它

您只需应用两条规则即可成功:仅将当前编号添加到nexts -如果他们是平等的 -如果它们不能被7整除

public static int specialSum(int a) {
    if(a <= 1) // Final Case.  
    {
        System.out.print(" 0 = ");
        return 0;
    }


    if(a%2 != 0) // Even Number, do not sum, call next step
    {
        return specialSum(a-1);
    }
    else 
    { 
        if(a % 7 == 0){ // Divisible by 7  Do not sum, call next step.
            return specialSum(a-1);
        }
        else // NOT divisible by 7 nor by 2, add it to the next step
        {
            System.out.print(a+ " + ");
            return a + specialSum(a-1);
        }

    }

}
publicstaticintspecialsum(inta){

如果(a这里的解在一行中,你应该有一个停止递归的例子,在你的例子中,你在49处停止递归,因为它可以被7整除,并且你不考虑小于49的数字

main()
{
    specialSum(50, 7);
}

 public static int specialSum(int a, int toDivide)
 {
    return (a == 0) ? 0 : (a >= 1 && a%2 == 0 && a%7 != 0) ? a + specialSum(a - 1, toDivide) : specialSum(a - 1, toDivide);
 }

在这里,一行中有一个解决方案,你应该有一个停止递归的案例,在你的案例中,你在49处停止递归,因为它可以被7整除,并且你不考虑小于49的数字

main()
{
    specialSum(50, 7);
}

 public static int specialSum(int a, int toDivide)
 {
    return (a == 0) ? 0 : (a >= 1 && a%2 == 0 && a%7 != 0) ? a + specialSum(a - 1, toDivide) : specialSum(a - 1, toDivide);
 }

您需要检查a是否可被7整除,因此您应该使用
if((a>=1)和&(a%7)!=0))
来确保您检查基本条件。

您需要检查a是否可被7整除,因此您应该使用
if((a>=1)和&(a%7)!=0))
以确保您检查基本条件。

递归意味着您继续迭代,直到达到最终条件。此条件是您的
else
,然后您只返回0。您可能希望将当前和传递到方法中。@ChrisWohlert无需将当前和传递到me中方法。检查下面的我的解决方案。好的,递归意味着你继续迭代,直到达到一个最终条件。这个条件是你的
else
,然后你只返回0。你可能想将当前和传递到方法中。@ChrisWohlert没有必要将当前和传递到方法中。检查下面的我的解决方案t使用值调用函数如何使用值调用函数这会引发StackOverflowException。您不能从中返回。这会引发StackOverflowException。您不能从中返回。