how to print this horizontally?

主宰稳场 提交于 2019-12-23 02:49:04

问题


Hey guys if you run this code with the given input you will get a vertical ruler i'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);
    }
}

回答1:


Assuming u want to do Ruler like this:

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

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:

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



回答2:


Here is a solution more inline with your example. You can still do it with recursion without having to use the power function to calculate the position of the tick.

I suggest to start with a smaller problem: How to draw the minor ticks between the major ticks. We can use the recursion for this one. We also have to realize that we can not anymore print the entire tick at once (as Fen1kz correctly mentioned) because it spans across multiple lines. We will have to loop over the number of lines and in the tick function either draw a tick or an empty space based on which line we are drawing. Once we have this we can easily draw one set of minor ticks. After that it is not too hard to add the rest of the code to repeat the previous and add the major ticks.

For completeness here is the whole code:

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

And the output is:

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


来源:https://stackoverflow.com/questions/25954584/how-to-print-this-horizontally

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!