Advertisement
jwrbg

PrintTriangle2

Feb 19th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. package com;
  2.  
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class PrintTriangle2
  7. {
  8.   public static void main(String[] args)
  9.   {
  10.     Scanner scanner = new Scanner(System.in);
  11.     int size;
  12.     String input;
  13.     //check input is positive number
  14.     do{
  15.  
  16.       while (!scanner.hasNextInt()) {
  17.         scanner.nextLine();
  18.       }
  19.       input = scanner.nextLine();
  20.       size = Integer.parseInt(input);
  21.  
  22.     }while(size <= 0);
  23.     int count = size;
  24.     int spaces = 0 ;
  25.     for (int i = 0; i <size ; i++) {
  26.       for (int j = 0; j <spaces ; j++) {
  27.         System.out.print("  ");
  28.  
  29.       }
  30.       for (int j = 0; j <count ; j++) {
  31.         System.out.print("# ");
  32.  
  33.       }
  34.       count--;
  35.       spaces++;
  36.       System.out.println();
  37.  
  38.  
  39.     }
  40.  
  41.  
  42.  
  43.  
  44.   }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement