Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

for loop - How to draw a staircase with Java?

I've been trying to get this done in Java. And this is a complex thing to draw, at least for me.

Q1 Write a simple Java program that prints a staircase or a figure as shown below:

                +---+
                |   |
            +---+---+
            |   |   |
        +---+---+---+
        |   |   |   |
    +---+---+---+---+
    |   |   |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+

I have come up with a solution but it is not even halfway there. This is the code I have come up with

public class DrawStairs {
    public static final int HEIGHT = 5;
    public static final int TOTALHEIGHT = HEIGHT * 5;
    public static void main(String[] args) {
        //Main Outer Loop
        for (int i = 1; i <= HEIGHT; i++) {
            //Loop for the spaces before, then print the head
            for (int j = 1; j <= TOTALHEIGHT + (i * (-5)); j++) {
                System.out.print(" ");
            }
            printTop();
            //Loop for spaces after, then print asterisk
            for (int j = 1; j <= (i - 1); j++) {
                System.out.print("---+");
            }
            System.out.println(" ");
            //Loop for the spaces before, then print the body
            for (int j = 1; j <= TOTALHEIGHT + (i * (-5)); j++) {
                System.out.print(" ");
            }
            printMiddle();
            //Loop for spaces after, then print asterisk
            for (int j = 1; j <= (i - 1) * 5; j++) {
                System.out.print(" ");
            }
            //Loop for spaces before, then print the legs
            for (int j = 1; j <= TOTALHEIGHT + (i * (-5)); j++) {
                System.out.print(" ");
            }
            printBottom();
            //Loop for spaces after, then print asterisk
            for (int j = HEIGHT; j <= 0; --j) {
                System.out.print("---+");
            }
            System.out.println("|");
        }
        // for loop for printing the floor of asterisks
        for (int i = 1; i <= HEIGHT; i++) {
            System.out.print("+---+");
        }
    }
    public static void printTop() {
        System.out.print("+---+");
    }
    public static void printMiddle() {
        System.out.print("|   |");
    }
    public static void printBottom() {
        // System.out.print("+---+");
    }
}

And this is what it does.

                    +---+ 
                    |   |                    |
               +---+---+ 
               |   |                    |
          +---+---+---+ 
          |   |                    |
     +---+---+---+---+ 
     |   |                    |
+---+---+---+---+---+ 
|   |                    |
+---++---++---++---++---+

Can anyone please help me and guide me with my code? I'd like if someone can tell me what's wrong and what should be changed.

question from:https://stackoverflow.com/questions/65934021/how-to-draw-a-staircase-with-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is my solution to the problem. Thanks for the puzzle ;)

public class Staircase {
    public static final int SIZE = 5;
    public static final int STAIR_WIDTH = 5;
    public static final String TREAD = "-";
    public static final String RISER = "|";
    public static final String NOSING = "+";
    public static final String HOLLOW = " ";

    public static void main(String[] args)
    {
        StringBuilder step = new StringBuilder();
        for (int i = 0; i < (STAIR_WIDTH - 2); ++i) { step.append(TREAD); }
        StringBuilder hollow = new StringBuilder();
        for (int i = 0; i < (STAIR_WIDTH - 2); ++i) { hollow.append(HOLLOW); }

        StringBuilder tread = new StringBuilder();
        for (int i = 0; i < SIZE; ++i) { tread.append(NOSING + step); }
        tread.append(NOSING);

        StringBuilder riser = new StringBuilder();
        for (int i = 0; i < SIZE; ++i) { riser.append(RISER + hollow); }
        riser.append(RISER);

        for (int i = 0; i < SIZE; ++i) {
            int offset = tread.length() - (((STAIR_WIDTH - 1) * i) + STAIR_WIDTH);
            printSpaces(offset);
            System.out.println(tread.substring(offset));
            printSpaces(offset);
            System.out.println(riser.substring(offset));
        }
        System.out.println(tread);
    }

    public static void printSpaces(int count)
    {
        for (int i = 0; i < count; ++i)
            System.out.print(" ");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...