我试图在java中水平打印这个标尺

时间:2014-09-19 21:26:17

标签: java rulers

我是java的新手,我正在尝试水平而不是垂直打印英文标尺,感谢任何帮助。

我尝试输出样本但我需要10个声望,但它与英国统治者非常相似。以下是照片http://i.stack.imgur.com/y8beS.jpg

的链接
public class MyRuler
{

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

    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("|\n");
        if (tickLabel >= 0)
            System.out.print(" " + tickLabel + "\n");
    }

    public static void drawTicks(int tickLength)
    { // draw ticks of given length
        if (tickLength > 0)
        { // stop when length drops to 0
            drawTicks(tickLength - 1); // recursively draw left ticks

            drawOneTick(tickLength); // draw center tick

            drawTicks(tickLength - 1); // recursively draw right ticks
        }
    }

    public static void drawRuler(int nInches, int majorLength)
    { // draw ruler
        drawOneTick(majorLength, 0); // draw tick 0 and its label
        for (int i = 1; i <= nInches; i++)
        {
            drawTicks(majorLength - 1); // draw ticks for this inch
            drawOneTick(majorLength, i); // draw tick i and its label
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您不打算使用特殊的演示公式(即,这可能不会缩放到实际标尺)并且只希望输出水平打印,则从代码中删除\n的所有实例它打印成一行。

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);

}

答案 1 :(得分:0)

即使看了你的样本图片,我也不确定你想要打印的是什么,所以我决定打印下面标尺的顶部:

enter image description here

考虑到我是欧洲人,我认为帝国制度很奇怪而且是一个重大的矫枉过正,我的统治者将在公制系统中测量:)(厘米和毫米)

好的,所以基本的想法是将每行标记或标签分开,就像它自己的String一样:

String1 = | | | | | | | | | | | | | | | | | | | | | | | ...   // regular ticks
String2 = |                   |                   |     ...   // ticks to labels
String3 = 0                   1                   2           // labels

我们分别构建每个字符串,然后我们将它们与新行'\n'字符组合在一起,以便它们正确打印。您还必须确保空格的数量是精确的,以便字符串正确对齐。

以下是代码:

class MyRuler {

    StringBuilder ticks = new StringBuilder();
    StringBuilder ticksToLabels = new StringBuilder();
    StringBuilder labels = new StringBuilder();

    int millimetersPerCentimeter = 10;

    String drawRuler(int centimeters) {
        // append the first tick, tick to label, and label
        ticks.append("| ");
        ticksToLabels.append("| ");
        labels.append(0);

        for(int i = 0; i < centimeters; i++) {
            for(int j = 0; j < millimetersPerCentimeter; j++) {
                if(j == millimetersPerCentimeter - 1) {
                    ticksToLabels.append("| ");
                    labels.append(" " + (i + 1));
                } else {
                    ticksToLabels.append("  ");
                    labels.append("  ");
                }
                ticks.append("| ");
            }
        }       
        ticks.append("\n" + ticksToLabels.toString() + "\n" + labels.toString());
        return ticks.toString();
    }

    public static void main(String[] args) {
        MyRuler ruler = new MyRuler();
        System.out.println(ruler.drawRuler(5));
    }
}

输出:

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