递归 实例 english ruler

  • For each inch, we place a tick with a numeric label. We denote the length of the tick designating a whole inch as the major tick length. Between the marks for whole inches, the ruler contains a series of minor ticks, placed at intervals of 1/2 inch, 1/4 inch, and so on. As the size of the interval decreases by half, the tick length decreases by one.
public static void drawRuler(int length, int tick) {
    drawLine(tick, 0);
    for (int i = 1; i <= length; i++) {
        drawInterval(tick - 1);
        drawLine(tick, i);
    }
}

private static void drawLine(int tickNum, int num) {
    for (int i = 0; i < tickNum; i++) {
        System.out.print("- ");
    }
    if (num >= 0) {
        System.out.println(num);
    } else {
        System.out.println();
    }

}

private static void drawInterval(int centralTick) {
    if (centralTick == 1) {
        drawLine(centralTick, -1);
    } else {
        drawInterval(centralTick - 1);
        drawLine(centralTick, -1);
        drawInterval(centralTick - 1);
    }
}