0

我是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
        }
    }
}
4

2 回答 2

0

即使在查看了您的示例图片后,我也不确定您要打印的确切内容,因此我决定在下面打印标尺的顶部:

在此处输入图像描述

考虑到我是欧洲人,我认为英制很奇怪,而且有点矫枉过正,我的尺子会用公制测量:)(厘米和毫米)

好的,所以基本思想是将每一行刻度或标签分开,就像它自己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
于 2014-09-20T00:05:07.200 回答
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);

}
于 2014-09-19T21:34:09.573 回答