borkins

Exam Jan-2016 - 10. Rectangle with Stars

May 11th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. /**
  2.  * Project: Exam_101_January_2016 - created by borkins on 2017-05-03.
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _10_RectangleWithStars
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner console = new Scanner(System.in);
  12.        
  13.         int n = Integer.parseInt(console.nextLine());
  14.        
  15.         int rows = n + ((n % 2 == 0) ? 1 : 2);
  16.         int cols = n * 2;
  17.         int innerSp = (n * 2) - 2;
  18.         int centerSp = (innerSp / 2) - 1;
  19.        
  20.         for (int i = 1; i <= rows; i++)
  21.         {
  22.             if (i == 1 || i == rows) {
  23.                 System.out.println(newString("%", cols));
  24.                 continue;
  25.             }
  26.             System.out.print("%");
  27.            
  28.             if (i != (rows - (rows / 2))) {
  29.                 System.out.print(newString(" ", innerSp));
  30.             }
  31.             else {
  32.                 System.out.printf("%s**%s",
  33.                                   newString(" ", centerSp),
  34.                                   newString(" ", centerSp));
  35.             }
  36.             System.out.println("%");
  37.         }
  38.     }
  39.    
  40.     public static String newString(String str, int count)
  41.     {
  42.         StringBuilder sb = new StringBuilder();
  43.        
  44.         for (int i = 0; i < count; i++) {
  45.             sb.append(str);
  46.         }
  47.         return sb.toString();
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment