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

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

    This can give you solution of your question.

    class Seven 
    {
    public static void main(String arg[])
    {
    for(int i=0;i<=8;i++)
    {
    for(int j=8;j>=i;j--)
    {
    System.out.print(" ");
    }
    for(int k=1;k<=(2*i+1);k++)
    {
    System.out.print("*");
    }
    System.out.println("\n");
    }
    }
    }
    

提交回复
热议问题