Home > Blockchain >  How to make horizonal star patterns?
How to make horizonal star patterns?

Time:12-06

If I want to make below picture likes horizonal star patterns, how to make it?

I want to make this :

  *           *
 ***         ***
*****       *****
 ***         ***
  *           *

But in my code print like this (It doesn't works)

  *
 ***
*****
 ***
  *
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

here is my code I try to make first picture to use Stringbuiler, It doesn't works.. Please tell me any idea.

    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        test(3,stringBuilder);
        test(5,stringBuilder);
        print(stringBuilder);
    }

    public static void test(int n,StringBuilder stringBuilder){
        for(int i=0;i<n;i  )
        {
            for(int j=0;j<n-i-1;j  ){
                stringBuilder.append(" ");
            }
            for(int k=0;k<2*i 1;k  ){
                stringBuilder.append("*");
            }
            stringBuilder.append(System.getProperty("line.separator"));
        }

        for(int i=n-1;i>0;i--)
        {
            for(int j=n-1;j>=i;j--){
                stringBuilder.append(" ");
            }
            for(int k=2*i-1;k>0;k--){
                stringBuilder.append("*");
            }
            stringBuilder.append(System.getProperty("line.separator"));
        }
    }

    public static void print(StringBuilder stringBuilder){
        System.out.println(stringBuilder);
    }

CodePudding user response:

You must change the way they are doing the algorithm. You must first get everything you want to print, and then paint them line by line.

CodePudding user response:

You are appending a line separator after every line of *'s.

stringBuilder.append(System.getProperty("line.separator"))

This means that when it's building the second diamond shape, it's starting on the next line below the bottom of the first diamond. The second diamond will be below the first diamond because of this.

You will need to build the two diamonds at the same time, e.g. append the 1st line of *'s for the first diamond, then the 1st line of *'s for the second diamond, then a new line. Repeat until all lines have been appended.

CodePudding user response:

public static void main(String[] args) {
 StringBuilder stringBuilder = new StringBuilder();
 test(3, stringBuilder);
 test(5, stringBuilder);
 print(stringBuilder);
 }

public static void test(int n, StringBuilder stringBuilder) {
// Print the upper part of the star pattern
for (int i = 0; i < n; i  ) {
// Insert spaces at the beginning and end of the row
stringBuilder.insert(0, " ");
stringBuilder.append(" ");


// Insert stars in the row
for (int k = 0; k < 2 * i   1; k  ) {
    stringBuilder.append("*");
}

// Insert newline at the end of the row
stringBuilder.append(System.getProperty("line.separator"));
}

 // Insert spaces between the upper and lower parts of the star pattern
 for (int i = 0; i < n - 1; i  ) {
   stringBuilder.append(" ");
}

// Print the lower part of the star pattern
for (int i = n - 1; i > 0; i--) {
// Insert spaces at the beginning and end of the row
stringBuilder.insert(0, " ");
stringBuilder.append(" ");

// Insert stars in the row
for (int k = 2 * i - 1; k > 0; k--) {
    stringBuilder.append("*");
}

// Insert newline at the end of the row
stringBuilder.append(System.getProperty("line.separator"));
}
}

public static void print(StringBuilder stringBuilder) {
 System.out.println(stringBuilder);
}
  •  Tags:  
  • java
  • Related