user3458666
user3458666

Reputation: 31

Print star using star pattern Java

I want to print the below pattern

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

I am able to print the two outer pyramids in the pattern using the below code:

    public static void main(String[] args) {
    int n = 8;
    int i, j;

    for (i = 1; i <= n; i++) {
        for (j = i; j < n; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        System.out.print("\n");

    }
    for (i = n; i >= 1; i--) {
        for (j = i; j < n; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.print("\n");
    }
}

This prints the pattern as

       *               
      * *              
     *   *             
    *     *             
   *       *           
  *         *              
 *           *
***************
***************
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *``

But how to combine these two to form a star shape?

Upvotes: 3

Views: 1416

Answers (3)

walen
walen

Reputation: 7273

Since you didn't specify what your restrictions were...

public static void main(String[] args) {
    System.out.println("       *       ");
    System.out.println("***************");
    System.out.println(" *   *   *   * ");
    System.out.println("  * *     * *  ");
    System.out.println("   *       *   ");
    System.out.println("  * *     * *  ");
    System.out.println(" *   *   *   * ");
    System.out.println("***************");
    System.out.println("       *       ");
}

Now obviously that is not what you intend to do, but next time it would be of help if you are more specific about your requirements, i.e.: "I have to do it using only for loops", "I'm not allowed to print more than a character at a time", "the code must be able to print any star of a given width", etc.
Because if none of those restrictions are in place, then the best way is just to print the ASCII lines like above.

Upvotes: 2

lgdroid57
lgdroid57

Reputation: 699

It seems to me that your loops are the same, except that one code block is ascending and one code block is descending. You could probably combine the twp code blocks, and use a third integer, k, to handle the other pattern.

public static void main(String[] args) {
    int n = 8;
    int i, j, k;

    for (i = 1; i <= n; i++) {
        k = n - i;

        for (j = i; j < n; j++) {
            if (/* TODO: handle k case */) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else if (/* TODO: handle k case */) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        System.out.print("\n");
    }
}

Make sense?

Upvotes: 0

theEpsilon
theEpsilon

Reputation: 1919

You could temporarily store the printing in a 2D char array and modify it twice, then use a for loop to print the array.

public static void main(String[] args) {
int n = 8;
char[][] temp = new char[][];
Arrays.fill(temp, ' ');
int i, j;

for (i = 1; i <= n; i++) {
    for (j = 1; j <= (2 * i - 1); j++) {
        if (i == n || j == 1 || j == (2 * i - 1)) {
            temp[i][j] = '*';
        }
    }
}
for (i = n; i >= 1; i--) {
    for (j = 1; j <= (2 * i - 1); j++) {
        if (i == n || j == 1 || j == (2 * i - 1)) {
            temp[i][j] = '*';
        }
    }
}

for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
        System.out.print(temp[i][j]);
    System.out.println();
}

}

Upvotes: 1

Related Questions