首页 > 解决方案 > 如何将值从递归方法传递给调用它的方法?

问题描述

我正在从方法(比如说方法 a)调用递归方法(比如说方法 b)。现在,在方法 b 完成计算结果后,我希望它将值返回给方法 a。我应该如何进行?

我尝试在方法 b 内部调用方法 a ,但不能这样做,因为它会破坏方法 b 的流程。

public long supercheck(long ognums) {
    if (ognums < 10) {
        return ognums;
    } else
        return sums(ognums);
}

public long sums(long ognums) {
    if (ognums == 0)
        return 0;
    return ognums % 10 + sums(ognums / 10);
}

在这里,我想将 sums 方法的返回值传递给 supercheck 方法。

标签: javarecursion

解决方案


推荐阅读