如何水平打印这个

如何水平打印这个

本文介绍了如何水平打印这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

嘿伙计们,如果您使用给定的输入运行此代码,您将获得一个垂直标尺我正在尝试使用给定的递归函数获得水平标尺,不知道如何到达那里或提示???

Hey guys if you run this code with the given input you will get a vertical ruleri'm trying to get a horizontal ruler using the given recursive functions any idea how to get there or hints ???

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 < tickLength; i++)
            System.out.print("-");
        if (tickLabel >= 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);
    }
}

推荐答案

假设你想像这样做 Ruler:

Assuming u want to do Ruler like this:

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

通过递归方法很难,因为你已经限制了 print/println(),而且因为文本是一维的 ^^因此,您将无法用 1 种方法绘制整个刻度线",不,高度为 N 的刻度线将采用 N+1 打印线.但是,正如你所看到的,我已经实现了这个标尺,只有循环:

It will be hard to it via recursive methods, because you've limited with print/println(), and because text is 1D ^^So you wont be able to draw whole "tickline" in 1 method, no, tickline with height N will take N+1 printlines.But, as you see, i've achieved this ruler, only with loops:

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

这篇关于如何水平打印这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:29