【发布时间】:2015-02-13 20:12:44
【问题描述】:
我想格式化我的 Java 程序输出,以便我可以看到递归的“深度”。怎么做? 不要迷失在我的递归树中,这一点非常重要。
示例输出(从 0 开始计算第 n 个数字的简单递归函数):
This is the first recursive call. Input value: 3.
This is the second recursive call. Input value: 2.
This is the 3rd recursive call. Input value: 1.
Output value : 1.
This is again the second recursive call. Input value: 2.
Output value : 1 + 1.
This is again the first recursive call. Input value: 3.
Output value : 1 + 1 + 1.
【问题讨论】:
-
最简单(如果不是最干净)的方法是简单地将
int depth作为参数添加到您的递归方法中,并在您的方法调用自身时增加它。然后,您可以使用它来确定要在输出字符串上添加多少个选项卡。
标签: java recursion tabs formatting