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

前端 未结 9 975
情话喂你
情话喂你 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:25

    You will need two for-loops; one to print the line, and one to print the characters in a line. The number of the current line can be used to print a certain number of stars.

    Use System.out.print("*") to print without adding a new line, at the end of the second loop do a System.out.println()

    I'll leave the implementation of the loops as an exercise, here is the syntax: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

提交回复
热议问题