Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main
- {
- public static void main (String[]args)
- {
- Scanner scan = new Scanner (System.in);
- int n = scan.nextInt ();
- int blankSpaces = n - 1;
- FirstAndLastLine(n);
- UpperTriangle(blankSpaces, n);
- blankSpaces = 1; // the first row of the LowerTriangle starts with 1 blank space
- int stars = n-1; // stars number for the LowerTriangle
- MiddleLine(n);
- LowerTriangle(blankSpaces, stars, n);
- FirstAndLastLine(n);
- }
- public static void FirstAndLastLine(int number){
- System.out.printf("%s%s", RepeatStr(" ", number), "*");
- System.out.println();
- }
- public static void UpperTriangle(int blanks, int number){
- for (int row = 1; row <= number-1; row++) // we use "row" as a star number because pur triangle is ascending
- {
- 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
- System.out.print("*"); // we make middle star to made a perfect triangular form
- System.out.printf("%s", RepeatStr("*", row)); // the other half of the row number
- System.out.println (); //new row
- blanks-=1; //blank spaces are going down with every step, to make triangular form
- }
- }
- public static void LowerTriangle(int blanks, int symbols, int number){
- for (int row = 1; row <= number-1; row++){
- 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
- System.out.print("*");// we make middle star to made a perfect triangular form
- System.out.printf("%s", RepeatStr("*", symbols)); // the other half of the row number
- System.out.println ();//new row
- blanks += 1; //blank spaces are going up with every step, to make triangular form
- symbols-=1; // the stars are going lower with every step, because our triangle is descending
- }
- }
- public static void MiddleLine(int number){
- System.out.printf("%s",RepeatStr("*", number*2 + 1));
- System.out.println();
- }
- public static String RepeatStr(String symbols, int n){
- String test = "";
- for (int i = 0; i < n; i++){
- test += symbols;
- }
- return test;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment