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
4.3k views
in Technique[技术] by (71.8m points)

java - Pyramid inside a rectangle pattern

I am having difficulty finding a suitable solution to stop the recurrence of blanks after the stars have finished printing, and this caused me a problem in printing Yemeni lines diverging, so I want them to be straight and similar to this pattern

enter image description here

public class task {

    public static void pettren1(int high, int width) {
        high = 11;
        width = 14;

        //Overline

        for (int i = 0; i <= width /2; i++) {
            System.out.print(" _");
        }
        System.out.println();
        for (int i = high; i > 0; i--) {
      //The left line and the space
            System.out.print("|");
            for (int j = high-1; j >= i; j--)
                System.out.print(" ");
            //for the stars
            for (int k = 1; k <= (i * 2 - 7); k++)
                System.out.print("*");

            //The right line and the space
            for (int j = high-1; j >= i; j--)
                System.out.print(" ");
            System.out.print("|");


            System.out.println();
        }
        //The bottom line
        for (int i = 0; i <= width /2; i++) {
            System.out.print(" _");
        }
    }

    public static void main(String[] args) {
        pettren1(0, 0);


    }

}

my output

enter image description here


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

1 Reply

0 votes
by (71.8m points)

You should have implemented a counter of printed characters and in the third nested loop printing spaces on the right side use this value to print spaces until width is reached:

for (int i = high; i > 0; i--) {
//The left line and the space
    System.out.print("|");
    int c = 0;
    for (int j = high-1; j >= i; j--) {
        System.out.print(" ");
        c++;
    }
    //for the stars
    for (int k = 1; k <= (i * 2 - 7); k++) {
        System.out.print("*");
        c++;
    }

    //The spaces and the right line
    for (int j = c; j <= width; j++) {
        System.out.print(" ");
    }
    System.out.print("|");

    System.out.println();
}

Also, it is not quite clear why "reverse" loops have been used. Incrementing loops can do the same:

for (int i = 0; i < high; i++) {
    int c = 0;
//The left line and the space
    System.out.print("|");
    
    for (int j = 0; j < i; j++) {
        System.out.print(" ");
        c++;
    }
    //for the stars
    for (int k = 0; k < width - 2 * i + 1; k++) {
        System.out.print("*");
        c++;
    }

    //The right line and the space
    for (int j = c; j <= width; j++) {
        System.out.print(" ");
    }
    
    System.out.print("|");

    System.out.println();
}

Both codes print the same pyramid:

 _ _ _ _ _ _ _ _
|***************|
| ************* |
|  ***********  |
|   *********   |
|    *******    |
|     *****     |
|      ***      |
|       *       |
|               |
|               |
|               |
 _ _ _ _ _ _ _ _

String::repeat-based solution calculating the number of stars beforehand:

for (int i = 0; i < high; i++) {
    int stars = Math.max(0, width - 2 * i + 1);
    System.out.printf("|%s%s%s|%n", " ".repeat(i), "*".repeat(stars), " ".repeat(width + 1 - i - stars));
}

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

...