2014-09-19 26 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