-4
以下のコードは、ユーザーに数字を入力するように要求し、合計を得るために数字のそれぞれを追加します。例えば、私が123を入力すると、(1 + 2 + 3)が実行され、6が出力されます。再帰的メソッドがあります: public static int sumDigits(long n)それがどのように機能するのか、長い宣言は何を意味するのでしょうか?あなたはこのような何かを探している再帰の仕組みを理解しようとしている
//this is my code
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a integer: ");
long n = input.nextLong();
// Display the sum of all the digits in the integer
System.out.println("The sum is: " + sumDigits(n));
}
//recrusve method that computes the sum of the digits in an integer
public static int sumDigits(long n) {
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
}
再帰関数は、それ自身を呼び出す関数です。 'sumDigits'は再帰関数ではありません。 – Mureinik
コードに再帰がありません。あなたが何を意味するのかを理解できなければ、再帰を理解することは非常に難しいでしょう。 – Gatusko
明らかにあなたの質問は 'oracle'とは関係ありません。 StackOverflow Webサイトがあなたの投稿に追加したタグを確認し、必要に応じて編集してください。今回はあなたのためにそれをやっています。 – mathguy