hpilo

Chapter4_Loops_Ex15

Dec 15th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3. ======================================
  4. chapter 4: Loops
  5.  
  6. Ex15: Christmas tree
  7. ======================================
  8. */
  9.  
  10. public class MyProgram {
  11. public static void main(String[] args) {
  12.  
  13. //variables
  14. int base;
  15. Scanner s=new Scanner(System.in);
  16.  
  17. //user input- bulletproof against stupid users
  18. do{
  19. System.out.println("Enter odd number for base: ");
  20. base=s.nextInt();
  21. }while(base%2==0);
  22.  
  23. //printing triangle base in bottom
  24. for(int row=0;row<base;row++) {
  25. for (int i = 0; i < base; i++) {
  26. for (int space = i + 1; space < base; space++) //loop for space
  27. System.out.print(" ");
  28. for (int j = base - i - 1; j < base; j++)
  29. System.out.print("* "); //loop for stars
  30. System.out.println();
  31. }
  32. }
  33.  
  34. //loop for tree trunk
  35. for(int trunk=0;trunk<base;trunk++) {
  36. for(int space=0; space<base/2;space++) //loop for space
  37. System.out.print(" ");
  38. System.out.println("*");
  39. }
  40.  
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment