fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7.  
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10.  
  11. class ChristmasTree {
  12.  
  13. ChristmasTree(){}
  14.  
  15. private final char treeSymbol = '*';
  16. private char[] ornaments = {'+', 'o', '$', '#'};
  17. StringBuilder tree = new StringBuilder();
  18.  
  19. public void buildRegDecoratedTree(int treeHight) {
  20.  
  21. //CREATE ROWS OF TREE LOOP
  22. for (int row = 0; row < treeHight; row++) {
  23. int maxWidth = (row * 2) + 1;
  24.  
  25. //ADDING INDENTATION LOOP
  26. for (int indentation = 0; indentation < treeHight - row - 1; indentation++) {
  27. tree.append(" ");
  28. }
  29.  
  30. //ADDING TREE SYMBOL WITH ORNAMENTS LOOP
  31. for (int idx = 0, idxo = 0; idx < (row*2)+1; ++idx)
  32. {
  33. if (idx%2 == 1)
  34. {
  35. tree.append(ornaments[(idxo++)%ornaments.length]);
  36. }
  37. else
  38. {
  39. tree.append(treeSymbol);
  40. }
  41. }
  42. tree.append(System.lineSeparator());
  43. }
  44. System.out.println(tree.toString());
  45. }
  46.  
  47. public static void main (String[] args) throws java.lang.Exception
  48. {
  49. ChristmasTree tree = new ChristmasTree();
  50. Scanner inPut = new Scanner(System.in);
  51.  
  52. System.out.println("Pass tree hight: ");
  53. int treeHight = inPut.nextInt();
  54. tree.buildRegDecoratedTree(treeHight);
  55.  
  56. inPut.close();
  57. }
  58. }
Success #stdin #stdout 0.07s 2184192KB
stdin
12
stdout
Pass tree hight: 
           *
          *+*
         *+*o*
        *+*o*$*
       *+*o*$*#*
      *+*o*$*#*+*
     *+*o*$*#*+*o*
    *+*o*$*#*+*o*$*
   *+*o*$*#*+*o*$*#*
  *+*o*$*#*+*o*$*#*+*
 *+*o*$*#*+*o*$*#*+*o*
*+*o*$*#*+*o*$*#*+*o*$*