atanasovetr

Rhombus

Dec 1st, 2019
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main
  4. {
  5.   public static void main (String[]args)
  6.   {
  7.     Scanner scan = new Scanner (System.in);
  8.     int n = scan.nextInt ();
  9.     int blankSpaces = n - 1;
  10.     FirstAndLastLine(n);
  11.     UpperTriangle(blankSpaces, n);
  12.     blankSpaces = 1; //  the first row of the LowerTriangle starts with 1 blank space
  13.     int stars = n-1; // stars number for the LowerTriangle
  14.     MiddleLine(n);
  15.     LowerTriangle(blankSpaces, stars, n);
  16.     FirstAndLastLine(n);
  17. }
  18.    
  19.     public static void FirstAndLastLine(int number){
  20.         System.out.printf("%s%s", RepeatStr(" ", number), "*");
  21.         System.out.println();
  22.     }
  23.     public static void UpperTriangle(int blanks, int number){
  24.     for (int row = 1; row <= number-1; row++) // we use "row" as a star number because pur triangle is ascending
  25.       {
  26.       System.out.printf("%s%s", RepeatStr(" ", blanks), RepeatStr("*", row)); //first we draw the blank spaces, next we draw the first half of the row number
  27.       System.out.print("*"); // we make middle star to made a perfect triangular form
  28.       System.out.printf("%s", RepeatStr("*", row)); // the other half of the row number
  29.       System.out.println ();  //new row
  30.       blanks-=1;  //blank spaces are going down with every step, to make triangular form
  31.       }
  32.     }
  33.     public static void LowerTriangle(int blanks, int symbols, int number){
  34.         for (int row = 1; row <= number-1; row++){
  35.         System.out.printf("%s%s", RepeatStr(" ", blanks), RepeatStr("*", symbols)); //first we draw the blank spaces, next we draw the first half of the row number
  36.         System.out.print("*");// we make middle star to made a perfect triangular form
  37.         System.out.printf("%s", RepeatStr("*", symbols)); // the other half of the row number
  38.         System.out.println ();//new row
  39.         blanks += 1; //blank spaces are going up with every step, to make triangular form
  40.         symbols-=1; // the stars are going lower with every step, because our triangle is descending
  41.         }
  42.     }
  43.    
  44.     public static void MiddleLine(int number){
  45.         System.out.printf("%s",RepeatStr("*", number*2 + 1));
  46.         System.out.println();
  47.     }
  48.    
  49.  
  50.     public static String RepeatStr(String symbols, int n){
  51.       String test = "";
  52.       for (int i = 0; i < n; i++){
  53.           test += symbols;
  54.       }
  55.       return test;
  56.   }
  57.  
  58.   }
Advertisement
Add Comment
Please, Sign In to add comment