Advertisement
patrickgh3

Easy 17

Mar 9th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Easy17 {
  4.     public static void main(String[] args) {
  5.        
  6.         // get input
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("  Triangle creator\n--------------------\nEnter a height: ");
  9.         int height = s.nextInt();
  10.         System.out.print("Reversed? (true/false) : ");
  11.         boolean reverse = (s.nextBoolean());
  12.         System.out.print("Right justified? (true/false) : ");
  13.         boolean rjust = (s.nextBoolean());
  14.        
  15.         if (!reverse) {
  16.             // output triangle regularly
  17.             int length = 1;
  18.             for (int i=0;i<height;i++) {
  19.                 if (rjust) for (int j=0;j<Math.pow(2,height-1)-length;j++) System.out.print(" ");
  20.                 for (int j=0;j<length;j++) System.out.print("@");
  21.                 length *= 2;
  22.                 System.out.println();
  23.             }
  24.         } else {
  25.             // output triangle in reverse
  26.             int length = (int)Math.pow(2, height-1);
  27.             for (int i=0;i<height;i++) {
  28.                 if (rjust) for (int j=0;j<Math.pow(2,height-1)-length;j++) System.out.print(" ");
  29.                 for (int j=0;j<length;j++) System.out.print("@");
  30.                 length /= 2;
  31.                 System.out.println();
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement