我正在尝试在Java中水平打印此标尺 [英] I'm trying to print this ruler horizontally in java

查看:29
本文介绍了我正在尝试在Java中水平打印此标尺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我想水平打印英文标尺,而不是垂直打印,感谢您的帮助.

我试图输出一个示例输出,但我需要10点声望,但这与英国尺子非常相似.这是照片的链接 http://i.stack.imgur.com/y8beS.jpg

 公共类MyRuler{公共静态void main(String [] args){drawRuler(3,3);}公共静态无效drawOneTick(int tickLength){drawOneTick(tickLength,-1);}//画一个勾公共静态无效drawOneTick(int tickLength,int tickLabel){为(int i = 0; i< tickLength; i ++)System.out.print("| \ n");如果(tickLabel> = 0)System.out.print(" + tickLabel +"\ n");}公共静态无效drawTicks(int tickLength){//绘制给定长度的刻度如果(tickLength> 0){//当长度减少到0时停止drawTicks(tickLength-1);//递归绘制左刻度线drawOneTick(tickLength);//绘制中心刻度drawTicks(tickLength-1);//递归绘制正确的刻度}}公共静态无效drawRuler(int nInches,int majorLength){//绘制标尺drawOneTick(majorLength,0);//绘制刻度线0及其标签为(int i = 1; i< = nInches; i ++){drawTicks(majorLength-1);//为此英寸绘制刻度drawOneTick(majorLength,i);//绘制刻度线i及其标签}}} 

解决方案

即使在查看示例图片后,我也不确定您要打印的内容是什么,所以我决定打印下面的标尺:

考虑到我是欧洲人,我认为帝国制很怪异并且是一个主要的过大杀伤力,我的尺子将在公制中进行测量:)(厘米和毫米)

好,所以基本思想是将刻度线或标签的每一行分隔为自己的 String ,例如:

  String1 = |||||||||||||||||||||||...//常规滴答字符串2 = |||...//在标签上打勾String3 = 0 1 2//标签 

我们分别构建每个字符串,然后在它们之间加上换行符'\ n'以便将它们正确打印.您还必须确保空格的数量正确,以便字符串正确对齐.

这是代码:

  class MyRuler {StringBuilder ticks = new StringBuilder();StringBuilder ticksToLabels =新的StringBuilder();StringBuilder标签= new StringBuilder();intimetersPerCentimeter = 10;字符串drawRuler(int厘米){//附加第一个刻度,在标签上加上刻度,然后加上标签ticks.append("|");ticksToLabels.append("|");labels.append(0);for(int i = 0; i< cm; i ++){for(int j = 0; j  

输出:

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

I'm new to java and I'm trying to print an english ruler horizontally instead of vertically, any help is appreciated.

I tried to put a sample output but I need 10 reputation but it's very similar to an english ruler. Here is a link of a photo 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
        }
    }
}

解决方案

Even after looking at your sample picture I wasn't sure what you wanted to print exactly so I decided to print the top part of the ruler bellow:

Considering I am European and I think the imperial system is weird and a major overkill, my ruler will measure in the metric system :) (centimeters and millimeters)

Ok, so the basic idea is to separate each row of ticks or labels as it's own String like:

String1 = | | | | | | | | | | | | | | | | | | | | | | | ...   // regular ticks
String2 = |                   |                   |     ...   // ticks to labels
String3 = 0                   1                   2           // labels

We build each string separately, then we combine them with a newline '\n' character in between them so they will print properly. You have to also make sure the number of spaces is exact so that the strings align correctly.

Here is the code:

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));
    }
}

Output:

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

这篇关于我正在尝试在Java中水平打印此标尺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆