-2

Can someone help me solve the bellow?

I have attached two codes. Both should be identical from my comparison. One of them prints almost normal Xmas Tree (Code 2/Result Code 2) and the other one (Code 1/Result Code 1) prints only the right side of the tree. I do not understand why and how is this possible....

Also, can someone explain to me the process of how the code works? I understand it only a little bit. Still learning and I copied Code 2 and tried to replicated manually with Code 1.

Code 1:

public class XmasTree {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("How tall do you want it?");
        int height = input.nextInt();


        for (int i = 0; i < height; i++) {
            for (int j = 0; j < height -i; j++);{
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        for (int i = 0; i <= height; i++){
            for (int j = 0; j <= height; j++){
                System.out.print(" ");
            }
            for (int k = 1; k < 2; k++){
                System.out.print("*");
            }
            System.out.println();
        }
        input.close();
    }
}

Code 2:

public class XmasTree2 {

    public static void main(String[]args){

        Scanner input = new Scanner(System.in);
        System.out.println("How tall do you want your tree to be?");
        int height = input.nextInt();


        for(int i = 0; i < height; i++){
            for(int j = 0; j < height - i; j++){
                System.out.print(" ");
            }
            for(int k = 0; k <= i; k++){
                System.out.print("* ");
            }
            System.out.println();
        }
        for(int i = 0; i <= height; i++){
            for(int j = 0; j <= height; j++){
                System.out.print(" ");
            }
            for(int k = 1; k < 2; k++){
                System.out.print("*");
            }
            System.out.println();
        }
        input.close();
    }
}

Result Code 1:

How tall do you want it? 4

 * 
 * * 
 * * * 
 * * * * 
     *
     *
     *
     *
     *

Result Code 2:

How tall do you want your tree to be? 4

    * 
   * * 
  * * * 
 * * * * 
     *
     *
     *
     *
     *
cegredev
  • 1,485
  • 2
  • 11
  • 25

1 Answers1

1

You have an extra ; after your second for-loop.

Do you want to know how I discovered that? I put your code in an IDE and let it auto-format it for me. That way the mistake becomes clear.

This is why correct formatting is so important.

cegredev
  • 1,485
  • 2
  • 11
  • 25
  • Any chance you can help me by telling me how to trigger that auto-formatting so I can see the error? I am using IntelliJ 2020 version. I started programming literally 5 days ago on my own so my knowledge of using the tools is super limited and most tutorials are very focused on their own things rather than proper review of the IDE. – BorisVasilev Apr 26 '20 at 20:42
  • https://stackoverflow.com/questions/17879475/how-enable-auto-format-code-for-intellij-idea – cegredev Apr 26 '20 at 21:11