Use Java to print out six triangles. Well, in fact, you first need to know how to print out triangles, and then loop it again, and you will have six triangles.
Print out the triangle code:
public class Dengyao { public static void main(String[] args) { for(int i=1;i<=5;i++){//i--控制行 for(int j=5-i;j>=0;j--){//j--控制空格的数量 System.out.print(" "); } for(int k=1;k<=2*i-1;k++){//k--控制*的数量 System.out.print("*"); } System.out.println();//每循环一次换行 } } }
Result:
* *** ***** ******* *********
At this time, you only need to add another loop to the program and loop six times, so that there will be six triangles.
public class Dengyao { for(int n=0;n<6;n++) {//增加的for循环 for(int i=1;i<=5;i++){//i--控制行 for(int j=5-i;j>=0;j--){//j--控制空格的数量 System.out.print(" "); } for(int k=1;k<=2*i-1;k++){//k--控制*的数量 System.out.print("*"); } System.out.println();//每循环一次换行 } System.out.println(); } }
The above is the detailed content of How to create 6 triangles using java. For more information, please follow other related articles on the PHP Chinese website!