Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1.     public static void print(int h) {
  2.         int lineStartIndex = h / 2;
  3.         int lineEndIndex = h - 1;
  4.  
  5.         for (int lineIndex = 0; lineIndex < h; lineIndex++) {
  6.             // print line
  7.             for (int columnIndex = 0; columnIndex < h; columnIndex++) {
  8.                 if (columnIndex < lineStartIndex || columnIndex > lineEndIndex)
  9.                     System.out.print(' ');
  10.                 else if (columnIndex == lineStartIndex || columnIndex == lineEndIndex
  11.                         || lineIndex == 0 || lineIndex == h - 1)
  12.                     System.out.print('*');
  13.                 else
  14.                     System.out.print('#');
  15.             }
  16.  
  17.             if (lineIndex < h / 2 - 1) {
  18.                 lineEndIndex--;
  19.             }
  20.             else if (lineIndex == h / 2 - 1) {
  21.                 lineStartIndex = h / 2 - 1;
  22.                 lineEndIndex = h / 2 - 1;
  23.             }
  24.             else {
  25.                 lineStartIndex--;
  26.             }
  27.  
  28.             System.out.println();
  29.         }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement