Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归方法返回值(java)_Java_Recursion - Fatal编程技术网

递归方法返回值(java)

递归方法返回值(java),java,recursion,Java,Recursion,我必须写一个递归方法来求整数的和 这个方法工作得很好,但我不明白为什么它返回最后一个整数而不是总数 import java.util.Scanner; public class SommaCifreRicorsivo{ public static void main (String[] args){ System.out.printf("Inserire un numero: "); Scanner tastiera = new Scanner(Syst

我必须写一个递归方法来求整数的和

这个方法工作得很好,但我不明白为什么它返回最后一个整数而不是总数

import java.util.Scanner;

public class SommaCifreRicorsivo{
    public static void main (String[] args){
        System.out.printf("Inserire un numero: ");
        Scanner tastiera = new Scanner(System.in);
        int numero = tastiera.nextInt();
        int somma = 0;

        int risultato = eseguiSomma(numero,somma);
        System.out.println("La somma delle sue cifre è " + risultato);
    }

    public static int eseguiSomma(int numero, int somma){
        //Caso base
        if (numero < 10) {
            somma = somma + numero;
            System.out.println("Aggiungo la cifra " + numero + " alla somma, ottenendo " + somma);
            return (somma += numero);
        }

        //Chiamate ricorsive
        somma = somma + numero%10;
        System.out.println("Aggiungo la cifra " + (numero%10) + " alla somma, ottenendo " + somma);
        eseguiSomma((numero/10), somma);

        return somma;

    }
} 
只需将其更正为:

import java.util.Scanner;

public class SommaCifreRicorsivo{
    public static void main (String[] args){
        System.out.printf("Inserire un numero: ");
        Scanner tastiera = new Scanner(System.in);
        int numero = tastiera.nextInt();
        int somma = 0;

        int risultato = eseguiSomma(numero,somma);
        System.out.println("La somma delle sue cifre è " + risultato);
    }

    public static int eseguiSomma(int numero, int somma){
        //Caso base
        if (numero < 10) {
            somma = somma + numero;
            System.out.println("Aggiungo la cifra " + numero + " alla somma, ottenendo " + somma);
            return (somma += numero);
        }

        //Chiamate ricorsive
        somma = somma + numero%10;
        System.out.println("Aggiungo la cifra " + (numero%10) + " alla somma, ottenendo " + somma);

        // the error was on this line!
        somma += eseguiSomma((numero/10), somma);

        return somma;

    }
} 
您试图修改somma,但somma是一个原始类型,因此您只修改其本地副本,而somma保持不变。

somma是一个基本类型int,因此Java按值传递它。这意味着您要修改的值只是存储在每个递归调用的堆栈帧上的somma的副本,而不是最终返回值

因此,somma的论点完全没有必要。您应该返回递归调用的返回值加上新的数字。这提供了一个更简单的解决方案

public static int eseguiSomma(int numero){
    //Caso base
    if (numero < 10) {
        return numero;
    }

    //Chiamate ricorsive
    return numero % 10 + eseguiSomma(numero / 10);
}

// now simply call without somma
int risultato = eseguiSomma(numero);

是的,对不起,我写了=而不是+=,马上更正我的答案,谢谢!