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