Advertisement
peterbodlev

Diamond

Jun 1st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 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.         int n = Integer.parseInt(scanner.nextLine());
  7.  
  8.         if (n % 2 == 0) {
  9.             for (int i = 0; i < n - 1; i++) {
  10.                 if (i <= n / 2 - 1) {
  11.                     String first = repeatStr("-", (n / 2 - 1) - i) + "*" + repeatStr("-", i * 2) + "*" + repeatStr("-", n / 2 - 1 - i);
  12.                     System.out.println(first);
  13.                 } else {
  14.                     String first = repeatStr("-", i - n / 2 + 1) + "*" + repeatStr("-", (n - 2 - i) * 2) + "*" + repeatStr("-", i - n / 2 + 1);
  15.                     System.out.println(first);
  16.                 }
  17.             }
  18.         } else {
  19.             String second1 = repeatStr("-", n / 2) + "*" + repeatStr("-", n / 2);
  20.             System.out.println(second1);
  21.             for (int i = 1; i <= n - 2; i++) {
  22.                 if (i <= n / 2) {
  23.                     String second = repeatStr("-", n / 2 - i) + "*" + repeatStr("-", (i*2) - 1) + "*" + repeatStr("-", n / 2 - i);
  24.                     System.out.println(second);
  25.                 }else {
  26.                     String second = repeatStr("-", i - n / 2 ) + "*" + repeatStr("-", (n - 2 - i) * 2 +1) + "*" + repeatStr("-", i - n / 2);
  27.                     System.out.println(second);
  28.                 }
  29.             }
  30.             if (n != 1)
  31.                 System.out.println(second1);
  32.         }
  33.     }
  34.  
  35.     static String repeatStr(String text, int count) {
  36.         StringBuilder result = new StringBuilder();
  37.         for (int i = 0; i < count; i++) {
  38.             result.append(text);
  39.         }
  40.         return result.toString();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement