2014-09-19 2 views
0

나는 자바가 처음이다. 나는 영어 통치자를 세로 대신 가로로 인쇄하려고 노력하고있다.나는이 통치자를 자바로 수평으로 인쇄하려하고있다.

샘플 출력을 시도했지만 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

제안 : 여기

코드입니다, (나는 그것을이 시간을 한) 코드 형식은 코드에서 원하는 출력을 제공하고 규칙을 읽어 Stack Overflow에 관한 질문 (How to Ask)을 게시하는 것에 대해 –

+0

'\ n'을 제거하고 | 함께 - 아마도 – bdavies6086

+0

| | | | | | | | | | | | | | | | | 여기에 샘플 출력을 수행했습니다. 가장 작은 표시는 1/2^

답변

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 인치 눈금자를 상상해보십시오. –

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

멋진 결과물처럼 보이지만 원본 코드의 생각은 똑같은 재귀 함수를 사용하여 수직으로하는자를 만드는 것이지만 수평으로 필요하므로 그냥 알아낼 수없는 트릭이 있습니다. –