Advertisement
borkins

10. Diamond

Apr 5th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. /**
  2.  * Project: Drawing_With_Loops - created by borkins on 2017-04-03.
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _10_Diamond
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner console = new Scanner(System.in);
  12.         StringBuilder outer = new StringBuilder();
  13.         StringBuilder inner = new StringBuilder();
  14.        
  15.         int n = Integer.parseInt(console.nextLine());
  16.        
  17.         boolean isEven = (n % 2 == 0);
  18.         int rows = n - (isEven ? 1 : 0);
  19.        
  20.         outer.append(new String(new char[rows / 2]).replace("\0", "-"));
  21.         inner.append(isEven ? "--" : "-");
  22.        
  23.         String peak = outer + (isEven ? "**" : "*") + outer;
  24.        
  25.         for (int row = 0; row < rows; row++)
  26.         {
  27.             if (row == 0 || row == n - 1) {
  28.                 System.out.println(peak);
  29.                 continue;
  30.             }
  31.            
  32.             if (row <= n / 2 && outer.length() > 0) {
  33.                 outer.setLength(outer.length() - 1);
  34.                 if (row > 1) inner.append("--");
  35.             }
  36.             else {
  37.                 outer.append("-");
  38.                 inner.setLength(inner.length() - 2);
  39.             }
  40.             System.out.println(outer + "*" + inner + "*" + outer);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement