2014-09-19 35 views
0

我是java新手,我试图水平打印英文标尺,而不是垂直打印,任何帮助都会被赞赏。我试图在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

建议:格式化你的代码(我做了这个时间),为您提供从代码需要的输出,并读取规则关于在堆栈溢出中发布问题(如何问) – 2014-09-19 21:31:28

+0

删除'\ n'并替换|与 - 可能是 – bdavies6086 2014-09-19 21:34:50

+0

| | | | | | | | | | | | | | | | | 我在这里做 样本输出的公式是最小的标记以1/2^ 2014-09-19 21:41:03

回答

0

如果你不为AA呈现特殊配方行进的链接(即这可能不会被缩放到实际的统治者),只是所要的输出水平打印,删除您代码中的所有\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); 

} 
+0

是的我试过了,但看起来并不像尺子 想象一下3英寸的尺子,例如这就是输出需要的尺寸看起来像 – 2014-09-19 21:46:15

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 
+0

不错的输出,但原始代码的想法是使用相同的递归函数来创建它垂直的标尺,但我需要水平,所以有一个技巧,我只是无法弄清楚 – 2014-09-20 02:47:24