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

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

    for(int aj =5;aj>=1;aj--){
    
                for (int a1 = 0; a1 < aj; a1++) {
                    System.out.print(" ");
                }
                        for(int a2 = 5;a2>=aj;a2--) {
                            System.out.print("$");
                        }
                    for(int a2 = 5;a2>aj;a2--) {
                        System.out.print("$");
                    }
                System.out.println();
    

提交回复
热议问题