如何水平打印?

时间:2014-09-21 00:27:41

标签: java recursion

嘿,如果你用给定的输入运行这个代码,你会得到一个垂直标尺 我试图使用给定的递归函数得到一个水平标尺任何想法如何到达那里或暗示???

public class Ruler {
    // draw a tick with no label 
    public static void drawOneTick(int tickLength) {
        drawOneTick(tickLength, -1);
    }

    // draw one tick 
    public static void drawOneTick(int tickLength, int tickLabel) {
        for (int i = 0; i < tickLength; i++)
            System.out.print("-");
        if (tickLabel >= 0)
            System.out.print(" " + tickLabel);
        System.out.print("\n");
    }

    public static void drawTicks(int tickLength) {
        if (tickLength > 0) {
            drawTicks(tickLength-1);
            drawOneTick(tickLength);
            drawTicks(tickLength-1);
        }
    }

    public static void drawRuler(int nInches, int majorLength) {
        drawOneTick(majorLength, 0);
        for (int i = 1; i <= nInches; i++) {
            drawTicks(majorLength-1);
            drawOneTick(majorLength, i);
        }
    }

    public static void main(String[] args) {
        drawRuler(3,4);
    }
}

2 个答案:

答案 0 :(得分:3)

假设你想这样做标尺:

0               1               2               3               4               5               
|               |               |               |               |               |               
|       |       |       |       |       |       |       |       |       |       |       
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

通过递归方法很难,因为你限制了print / println(),因为文本是1D ^^ 所以你不能在1方法中绘制整个“tickline”,不,高度为N的刻度线将采用N + 1个印刷线。 但是,如你所见,我已经实现了这个统治者,只有循环:

SPOLER!

package com.company;

public class Main {
    private static String drawLabels(int count, int offset) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <= count; i++) {
            sb.append(i);
            for (int spaces = 0; spaces < offset; ++spaces) {
                sb.append(' ');
            }
        }
        return sb.toString();
    }

    private static String drawTicks(int count, int offset) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <= count; i++) {
            sb.append('|');
            for (int spaces = 0; spaces < offset; ++spaces) {
                sb.append(' ');
            }
        }
        return sb.toString();
    }

    public static void drawRuler(int nInches, int majorLength) {
        // labels part
        int offset = (int) Math.pow(2, majorLength - 1) - 1;
        System.out.println(drawLabels(nInches, offset));
        // rest
        for (int line = majorLength; line > 0; --line) {
            int ticksOffset = (int) Math.pow(2, line - 1) - 1;
            int ticksNumber = nInches * (int) Math.pow(2, majorLength - line);
            System.out.println(drawTicks(ticksNumber, ticksOffset));
        }
    }

    public static void main(String[] args) {
        drawRuler(5,5);
    }
}

答案 1 :(得分:1)

这是一个更符合您的示例的解决方案。您仍然可以使用递归来执行此操作,而无需使用幂函数来计算刻度线的位置。

我建议从一个较小的问题开始:如何在主要刻度之间绘制次要刻度。我们可以使用这个递归。我们还必须意识到我们不能再一次打印整个刻度(正如Fen1kz正确提到的那样)因为它跨越多行。我们将不得不循环遍历行数并在tick函数中根据我们绘制的线绘制刻度线或空白空间。一旦我们有了这个,我们就可以轻松地绘制一组次要的刻度。在此之后,添加其余代码以重复前一个并添加主要刻度并不太难。

为了完整性,这里是整个代码:

    public class Ruler {
    private static void drawMinorTicks(int line, int ticks) {
        if (ticks > 1) {
            drawMinorTicks(line, ticks - 1);
        }
        if (line <= ticks) {
            System.out.print('|');
        } else {
            System.out.print(' ');
        }
        if (ticks > 1) {
            drawMinorTicks(line, ticks - 1);
        }
    }

    private static void drawSingleMajorTick(int line, int ticks, int label) {
        if (line <= ticks) {
            System.out.print('|');
        } else {
            System.out.print(label);
        }
    }

    private static void drawMajorTicks(int inches, int line, int ticks) {
        drawSingleMajorTick(line, ticks, 0);
        for (int i = 1; i <= inches; i++) {
            drawMinorTicks(line, ticks - 1);
            drawSingleMajorTick(line, ticks, i);
        }
    }

    private static void drawRuler(int inches, int ticks) {
        for (int i = 1; i <= ticks + 1; ++i) {
            drawMajorTicks(inches, i, ticks);
            System.out.println();
        }
    }

    public static void main(String[] args) {
        drawRuler(5, 5);
    }
}

输出是:

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
|       |       |       |       |       |       |       |       |       |       |
|               |               |               |               |               |
0               1               2               3               4               5