1  Public classshanjiaoxing2 {3      Public Static voidMain (string[] args)4     {5          for(inti=0;i<5; i++ )6         {7              for(intJ=0;j<=5-i; j + +)//the output spaces are decremented with the loop8             {9System.out.print ("");Ten             } One              for(intk=0;k<=i*2; k++)//The output * number is incremented with the outer loop, and when k<=i prints only half of the triangle, the first K starts at 0. A             { -System.out.print ("*"); -             } the System.out.println (); -         } -     } -}
Operation Result:
Output Half triangle:
1  Public classshanjiaoxing2 {3      Public Static voidMain (string[] args)4     {5          for(inti=0;i<5; i++ )6         {7              for(intJ=0;j<=5-i; j + +)//the output spaces are decremented with the loop8             {9System.out.print ("");Ten             } One              for(intk=0;k<=i; k++)//The output * number is incremented with the outer loop, and when k<=i prints only half of the triangle, the first K starts at 0. Not the same place as above is I not multiplied by 2 A             { -System.out.print ("*"); -             } the System.out.println (); -         } -     } -}
Operation Result:
Another kind of triangle:
1  Public classshanjiaoxing2 {3      Public Static voidMain (string[] args)4     {5          for(inti=0;i<5; i++ )6         {7              for(intJ=0;j<=5-i; j + +)//the output spaces are decremented with the loop8             {9System.out.print ("");Ten             } One              for(intk=0;k<=i; k++)//The output * number is incremented with the outer loop, and when k<=i prints only half of the triangle, the first K starts at 0. A             { -System.out.print ("*");//a space is appended to the * number here -             } the System.out.println (); -         } -     } -}
The result of the operation is: (note) This is the upper half of the triangle plus a space derived
Java implementation asterisk triangle