TheBulgarianWolf

Diamond shape

Feb 29th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. class Main {
  4.   public static void main(String[] args) {
  5.     Scanner sc = new Scanner(System.in);
  6.     System.out.print("Enter the number of stars: ");
  7.     int n = Integer.parseInt(sc.nextLine());
  8.     //top
  9.     int stars;
  10.     int betweenDash;
  11.     if(n % 2 == 0){
  12.         stars = 2;
  13.         betweenDash = 2;
  14.     }
  15.     else{
  16.         stars = 1;
  17.         betweenDash = 1;
  18.     }
  19.     String dashes = generateFrom("-",(n-stars)/2);
  20.     String starSym = generateFrom("*",stars);
  21.     System.out.print(dashes);
  22.     System.out.print(starSym);
  23.     System.out.print(dashes);
  24.     System.out.println();
  25.     //under top
  26.     int sideDash;
  27.     if(n>4){
  28.         int k = 0;
  29.         if(n%2 == 0){
  30.             k = n/3+1;
  31.         }
  32.         else {
  33.             k=n/3;
  34.         }
  35.      
  36.         for(int i=0;i<(n-3)/2;i++){
  37.             sideDash = (n-(betweenDash+2))/2;
  38.             String side = generateFrom("-", sideDash);
  39.             String between = generateFrom("-", betweenDash);
  40.             System.out.print(side);
  41.             System.out.print("*");
  42.             System.out.print(between);
  43.             System.out.print("*");
  44.             System.out.print(side);
  45.             System.out.println();
  46.             betweenDash+=2;
  47.         }
  48.     }
  49.     //middle
  50.    
  51.     if(n>2 && n!=1){
  52.     String middleDash = generateFrom("-",n-2);
  53.     System.out.print("*");
  54.     System.out.print(middleDash);
  55.     System.out.print("*");
  56.     System.out.println();}
  57.     //under middle
  58.     betweenDash -= 2;
  59.     if(n>4){
  60.        
  61.         for(int i=(n-3)/2;i>0;i--){
  62.             sideDash = (n-(betweenDash+2))/2;
  63.             String side = generateFrom("-", sideDash);
  64.             String between = generateFrom("-", betweenDash);
  65.             System.out.print(side);
  66.             System.out.print("*");
  67.             System.out.print(between);
  68.             System.out.print("*");
  69.             System.out.print(side);
  70.             System.out.println();
  71.             betweenDash-=2;
  72.         }
  73.     }
  74.     //bottom
  75.     if(n>2){
  76.         System.out.print(dashes);
  77.         System.out.print(starSym);
  78.         System.out.print(dashes);
  79.         System.out.println();
  80.     }
  81.    
  82. }
  83.  
  84.  
  85.  
  86.   public static String generateFrom(String symbol, int numberOfStars){
  87.     StringBuffer builder = new StringBuffer();
  88.     for(int i = 0;i< numberOfStars;i++){
  89.       builder.append(symbol);
  90.     }
  91.     return builder.toString();
  92.   }
  93. }
Add Comment
Please, Sign In to add comment