How can i print a triangle with “*”s using for loop in java?

前端 未结 9 957
情话喂你
情话喂你 2021-01-29 10:35

I want to draw a triangle with stars like below using for loop, but i really don\'t have any idea of how to do this ? Triangle is going to be like this:

*
**
**         


        
9条回答
  •  情歌与酒
    2021-01-29 11:20

    Just to comment on your internet-found code...

    1. The for loops should always start from 0, unless you have a specific reason to start from 1. Its a good habit to practice starting from 0 for everything, as it'll help you when it comes to using java arrays.
    2. The 2 for loops inside each other... The outside loop is just controlling how many lines there are in the triangle (8 in this case). The inner loop is writing the number of stars for that line. This isn't the best way of achieving the result, but it would work correctly.
    3. The for loop at the bottom is writing out stars to appear like the trunk of a tree.

    Hope this helps your understanding.

提交回复
热议问题