Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. /*
  2. Pyramid implemented in Java.
  3. Author: Marcel
  4.  */
  5.  
  6.  
  7. import java.util.Scanner; // Import Scanner class for get user input
  8.  
  9. public class Pyramid {
  10.  
  11.         public static void main(String[] args)
  12.         {
  13.  
  14.         Scanner in = new Scanner(System.in); // Create a scanner object
  15.         System.out.print("Enter size of pyramid: "); //Prompt user for pyramid size
  16.         int size = in.nextInt(); // Initalization of  size variable, and read user input
  17.  
  18.         int oddNumber=1; // counter needed for changing rows by odd numbers
  19.         int numberOfSpaces=size-1; //number of spaces at start always less by 1 than initial size
  20.  
  21.         for(int i=1;i<=size;i++) { // responsible for rows
  22.  
  23.             char letter='`'; //this char is before a
  24.             for(int j=1;j<=numberOfSpaces;j++)
  25.             {
  26.                 System.out.print(" "); //print spaces
  27.             }
  28.  
  29.  
  30.             for (int j = 1; j <= oddNumber; j++) {
  31.  
  32.  
  33.                 if(j<=i)  //until j is less or equal as row number chars increment
  34.                 {
  35.                     letter++;
  36.                 }
  37.                 else //when j is more than row number chars decrement
  38.                 {
  39.                     letter--;
  40.                 }
  41.  
  42.                 System.out.print(letter); //print char
  43.             }
  44.             System.out.println();
  45.             oddNumber+=2; //grows by 2 each loop to ensure odd number of chars in row
  46.             numberOfSpaces--;//number of spaces decrease each loop
  47.         }
  48.  
  49.         }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement