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
*
* *
* * *
* * * *
*
*
*
*
*