Advertisement
Guest User

Pyramid.java

a guest
Apr 25th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1.  
  2. // drawing ascii triangles,
  3. // this program took some trouble to code
  4. // and had to install new Java compiler... :/
  5.  
  6. import java.util.Scanner;
  7.  
  8. class Pyramid {
  9.     // #wtf Java is such a bother...
  10.     // int width;
  11.     // int segments;
  12.  
  13.     static void repeat(char ch, int n) {
  14.         while(n-- > 0) System.out.print(ch);
  15.         return;
  16.     }
  17.  
  18.     static void drawRow(int width, int x) {
  19.         repeat(' ', x);
  20.         repeat('#', width-2*x);
  21.         System.out.println();
  22.         return;
  23.     }
  24.  
  25.     static void drawUpper(int width) {
  26.         int x, max = (width/2);
  27.         for(x = 0; x <= max; x++) drawRow(width, x);
  28.         return;
  29.     }
  30.  
  31.     static void drawLower(int width) {
  32.         int x, max = (width/2);
  33.         for(x = 0; x <= max; x++) drawRow(width, max-x);
  34.         return;
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         int i, width, segments;
  39.         Scanner sc = new Scanner(System.in);
  40.  
  41.         System.out.print("[width, segments] please: ");
  42.  
  43.         width = sc.nextInt();
  44.         segments = sc.nextInt();
  45.         sc.close();
  46.  
  47.         for(i = 0; i < segments; i++) {
  48.             if((i % 2) == 0) drawUpper(width);
  49.             else drawLower(width);
  50.         }
  51.         return;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement