Hello,
i am now trying to draw a tree with stars using for loop. I find a code for this but i did not get the logic of the code. Please can anybody explain it to me ? The code is below:

public class DrawingTree {
    public static void main(String[]args)
    {
         int n = 5; 
         int spaces = n-1;
         int ast;                 
         for(int i = 0; i < n; i++)
         {  
            ast = 2*i+1;
            for(int j = 1; j <= spaces+ast; j++)
            {
                if(j <= spaces)
                    System.out.print(' ');
                else
                    System.out.print('*');
            }
            System.out.println();
            spaces--;
        }    
    }
}

Add some printlns to print out the values of the variables to show you what the code is doing,
Another technique is to use a piece of paper and play computer. Write down the values of the variables as they are used and changed.

Edited 4 Years Ago by NormR1

Another technique is to use a piece of paper and play computer. Write down the values of the variables as they are used and changed.

I did but i did not understand. BTW whenever i ask a question, you just tell me to do something that everybody says. I think your comments have never been helpful for me. If you can help, please make a clear explanation. If you cannot, please don't comment on my posts. Thanks. You can ban me anymore just because you are moderator, right ?

I guess I just think you are smarter than you think you are. By taking some effort to solve a problem you will learn a liitle more.

If you have any specific questions about the code ask them. I've often worked out how code works by playing computer with a piece of paper and a pencil. That takes longer than having someone else do the work for you.

If you have any specific questions about the code ask them. I've often worked out how code works by playing computer with a piece of paper and a pencil. That takes longer than having someone else do the work for you.

I dont want anyone to do my work, but it is midnight in my country and i am about to be a psycho juse because of not being able to understand this stuff. I don't know anyone to ask my questions. You are the only ones who can help me learn java. As you always say, please be more specific.

If you take a piece of paper and write down the code for the program. The "play computer' by evaluating each statement starting at the top and writing down the value of the expression/variable as the program would do it.

     int n = 5; 
     int spaces = n-1;       //spaces=4
     int ast;                 
     for(int i = 0; i < n; i++) // i=0 | 1 | 2 | 3
     {  
        ast = 2*i+1;         // ast=1  | 3 | 5 | 7  

The | separate the values for each time around the loop. continue with the above logic through the rest of the code.

If you take a piece of paper and write down the code for the program. The "play computer' by evaluating each statement starting at the top and writing down the value of the expression/variable as the program would do it.

Soory for being mad at you. I have sent you a private message. :(

This question has already been answered. Start a new discussion instead.