skipter

Hourglass - Drawing with Loops

Apr 19th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(input.nextLine());
  9.  
  10.         System.out.println(repeatStr("*", n * 2 + 1));
  11.         System.out.println(".*" + repeatStr(" ", (n * 2 + 1) - 4) + "*.");
  12.  
  13.             for (int j = 0; j < n - 2; j++) {
  14.                 System.out.println(repeatStr(".", (2 + j)) + "*" + repeatStr("@",((n -j)*2) - 5)
  15.                      + "*" + repeatStr(".", (2 + j)));
  16.         }
  17.         System.out.println(repeatStr(".", n) + "*" + repeatStr(".", n));
  18.         System.out.println(repeatStr(".", n - 1) + "*@*" + repeatStr(".", n - 1));
  19.         for (int i = 0; i < n - 3; i++) {
  20.             System.out.println(repeatStr(".",(n - 2) - i ) + "*" + repeatStr(" ",1 + i)
  21.                     + "@" + repeatStr(" ", 1 + i) + "*" + repeatStr(".", (n - 2) - i));
  22.         }
  23.         System.out.println(".*" + repeatStr("@",((n * 2) + 1) - 4 ) + "*.");
  24.         System.out.println(repeatStr("*", n * 2 + 1));
  25.  
  26.  
  27.     }
  28.     public static String repeatStr(String repeatString, int counter) {
  29.         String text = "";
  30.         for (int i = 0; i < counter; i++) {
  31.             text += repeatString;
  32.         }
  33.         return text;
  34.     }
  35. }
Add Comment
Please, Sign In to add comment