使用递归进行输出缩进

huangapple go评论65阅读模式
英文:

Indenting an output with recursion

问题

public class Practice1 {
    /**
     * 每次递归打印出两行文本,并在每次递归时增加缩进(示例在javadoc下方)。我知道使用 `(numCalls + 1)` 可以使缩进逐渐增加,但我不确定如何将所有部分组合起来以实现题目要求的功能。
     *
     * 对于每次递归调用,应该打印出2行文本,指示调用编号
     * (一个大于等于1且小于等于numCalls的值,如下所示)。
     * 每层递归应该通过在输入行前添加r个空格来表示缩进。
     * 例如,如果numCalls为3,则方法应打印:
     *  call 1
     *   call 2
     *    call 3
     *    back 3
     *   back 2
     *  back 1
     *  
     * @param r        方法调用的层级
     * @param numCalls 所需的层级数
     */
    public static void stairSteps(int r, int numCalls) {
        if (r == 0) {
            System.out.println("Done");
        } else {
            System.out.println("call " + r);
            System.out.println("back " + r);
            r--;
            numCalls++;
            stairSteps(r, numCalls);
        }
    }
}
英文:

I am trying to use recursion to print out two lines of text each and with an indent every time there's recursion (example in javadoc below). I know using (numCalls + 1) will allow it to increase the indent each time but I'm not sure how to actually put it together so does what it is asking for.

public class Practice1 {
/**
 * Prints 2 lines of text for each recursive call, indicating call number 
     * (a value >= 1, and <= value of numCalls, as illustrated below.  Each
     * level of recursion should be indicated by indenting the input line by
     * r spaces.  For example, if numCalls is 3, the method should print:
 *  call 1
 *   call 2
 *    call 3
 *    back 3
 *   back 2
 *  back 1
 *  @param r the level of method calls
 *  @param numCalls the number of intended levels
 */

public static void stairSteps(int r, int numCalls) {
    if (r == 0) {
        System.out.println("Done");
    } else {
        System.out.println("call " + r);
        System.out.println("back " + r);
        r--;
        numCalls++;
        stairSteps(r, numCalls);
    }
}

答案1

得分: 1

public static void stairSteps(int totalLevels, int indent) {
    stairSteps(1, totalLevels, indent);
    System.out.println("Done");
}

private static void stairSteps(int level, int totalLevels, int indent) {
    if (level <= totalLevels) {
        int offs = indent * (level - 1);
        System.out.println(" ".repeat(offs) + "call " + level);
        stairSteps(level + 1, totalLevels, indent);
        System.out.println(" ".repeat(offs) + "back " + level);
    }
}

**输出:**

stairSteps(3, 2);

call 1
  call 2
    call 3
    back 3
  back 2
back 1
Done
英文:
public static void stairSteps(int totalLevels, int indent) {
    stairSteps(1, totalLevels, indent);
    System.out.println(&quot;Done&quot;);
}

private static void stairSteps(int level, int totalLevels, int indent) {
    if (level &lt;= totalLevels) {
        int offs = indent * (level - 1);
        System.out.println(&quot; &quot;.repeat(offs) + &quot;call &quot; + level);
        stairSteps(level + 1, totalLevels, indent);
        System.out.println(&quot; &quot;.repeat(offs) + &quot;back &quot; + level);
    }
}

Output:

stairSteps(3, 2);

call 1
  call 2
    call 3
    back 3
  back 2
back 1
Done

huangapple
  • 本文由 发表于 2020年9月23日 02:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64015997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定