martinvalchev

Diamond

Nov 14th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Diamond {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.  
  9.         int dash = ( n - 2 ) / 2;
  10.         int mid = n - 4;
  11.  
  12.         if ( n == 1) {
  13.             System.out.println("*");
  14.             return;
  15.         }
  16.        
  17.         if (n % 2 == 0) {
  18.             for (int i = 0; i < n / 2 ; i++) {
  19.                 System.out.println(repeatStr("-", dash) + "*" +
  20.                         repeatStr("-", 2 * i) + "*" + repeatStr("-", dash));
  21.                 dash --;
  22.             }
  23.             dash = 1;
  24.  
  25.             for (int i = 0; i < (n / 2) - 1  ; i++) {
  26.                 System.out.println(repeatStr("-", dash) + "*" +
  27.                         repeatStr("-", mid) + "*" + repeatStr("-", dash));
  28.                 mid -= 2;
  29.                 dash ++;
  30.             }
  31.         }
  32.         else {
  33.             System.out.println(repeatStr("-", (n - 1)/2) + "*" + repeatStr("-", (n - 1)/2) );
  34.             for (int i = 0; i < n / 2 ; i++) {
  35.                 System.out.println(repeatStr("-", dash) + "*" +
  36.                         repeatStr("-", 2 * i + 1) + "*" + repeatStr("-", dash));
  37.                 dash --;
  38.             }
  39.             for (int i = 0; i < (n / 2) - 1  ; i++) {
  40.                 System.out.println(repeatStr("-", i + 1 ) + "*" +
  41.                         repeatStr("-", mid) + "*" + repeatStr("-", i + 1));
  42.                 mid -= 2;
  43.                 dash ++;
  44.             }
  45.             System.out.println(repeatStr("-", (n - 1)/2) + "*" + repeatStr("-", (n - 1)/2) );
  46.  
  47.         }
  48.     }
  49.  
  50.     static String repeatStr(String text, int count) {
  51.         StringBuilder sb = new StringBuilder();
  52.  
  53.         for (int i = 0; i < count; i++) {
  54.             sb.append(text);
  55.         }
  56.         return sb.toString();
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment