java - 如何将值从递归方法传递给调用它的方法?
问题描述
我正在从方法(比如说方法 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 方法。
解决方案
推荐阅读
- php - 如何在新窗口中安装 XAMPP 而不会丢失数据
- lua - ROBLOX LUA(u) 传递 table = nil 的参数(尝试将 nil 与字符串连接)
- go - 如何通过 websocket 上传文件?
- c# - 如何写一个
WPF XAML 的用户控件 - python - 超过了 Meilisearch 的最大重试次数
- php - 使用 PHP 的 Facebook 图形 API
- php - Symfony 自定义方法存储库无法在控制器中访问
- mysql - 需要 Gap 和 Island 的最后一个值
- angular - 在角度 11 中按列名排序如何在主 url 上将列显示为参数
- typescript - TS6137:从 @types 导入时无法导入类型声明文件