如何水平打印?

嘿大家,如果你用给定的输入运行这个代码你会得到一个垂直标尺我试图得到一个水平标尺使用给定的递归函数任何想法如何到达那里或提示???

public class Ruler { // draw a tick with no label 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 = 0) System.out.print(" " + tickLabel); System.out.print("\n"); } public static void drawTicks(int tickLength) { if (tickLength > 0) { drawTicks(tickLength-1); drawOneTick(tickLength); drawTicks(tickLength-1); } } public static void drawRuler(int nInches, int majorLength) { drawOneTick(majorLength, 0); for (int i = 1; i <= nInches; i++) { drawTicks(majorLength-1); drawOneTick(majorLength, i); } } public static void main(String[] args) { drawRuler(3,4); } } 

假设你想这样做标尺:

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

通过递归方法很难,因为你限制了print / println(),因为文本是1D ^^所以你不能在1方法中绘制整个“tickline”,不,高度为N的tickline将采用N + 1印刷线。 但是,如你所见,我已经实现了这个标尺,只有循环:

SPOLER!

 package com.company; public class Main { private static String drawLabels(int count, int offset) { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= count; i++) { sb.append(i); for (int spaces = 0; spaces < offset; ++spaces) { sb.append(' '); } } return sb.toString(); } private static String drawTicks(int count, int offset) { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= count; i++) { sb.append('|'); for (int spaces = 0; spaces < offset; ++spaces) { sb.append(' '); } } return sb.toString(); } public static void drawRuler(int nInches, int majorLength) { // labels part int offset = (int) Math.pow(2, majorLength - 1) - 1; System.out.println(drawLabels(nInches, offset)); // rest for (int line = majorLength; line > 0; --line) { int ticksOffset = (int) Math.pow(2, line - 1) - 1; int ticksNumber = nInches * (int) Math.pow(2, majorLength - line); System.out.println(drawTicks(ticksNumber, ticksOffset)); } } public static void main(String[] args) { drawRuler(5,5); } } 

这是一个更符合您的示例的解决方案。 您仍然可以使用递归来执行此操作,而无需使用幂函数来计算tick的位置。

我建议从一个较小的问题开始:如何在主要刻度之间绘制次要刻度。 我们可以使用这个递归。 我们还必须意识到我们不能再一次打印整个标记(正如Fen1kz正确提到的那样),因为它横跨多行。 我们将不得不循环遍历行数并在tick函数中根据我们绘制的线绘制刻度线或空白空间。 一旦我们有了这个,我们可以轻松地绘制一组次要的刻度。 之后,添加其余代码以重复前一个并添加主要刻度并不太难。

为了完整性,这里是整个代码:

  public class Ruler { private static void drawMinorTicks(int line, int ticks) { if (ticks > 1) { drawMinorTicks(line, ticks - 1); } if (line <= ticks) { System.out.print('|'); } else { System.out.print(' '); } if (ticks > 1) { drawMinorTicks(line, ticks - 1); } } private static void drawSingleMajorTick(int line, int ticks, int label) { if (line <= ticks) { System.out.print('|'); } else { System.out.print(label); } } private static void drawMajorTicks(int inches, int line, int ticks) { drawSingleMajorTick(line, ticks, 0); for (int i = 1; i <= inches; i++) { drawMinorTicks(line, ticks - 1); drawSingleMajorTick(line, ticks, i); } } private static void drawRuler(int inches, int ticks) { for (int i = 1; i <= ticks + 1; ++i) { drawMajorTicks(inches, i, ticks); System.out.println(); } } public static void main(String[] args) { drawRuler(5, 5); } } 

输出是:

 

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