Home > Java > javaTutorial > body text

How to create 6 triangles using java

(*-*)浩
Release: 2019-05-22 14:33:37
Original
3401 people have browsed it

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.

How to create 6 triangles using java

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();//每循环一次换行
        }
    }
}
Copy after login

Result:

     *
    ***
   *****
  *******
 *********
Copy after login

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();
	    	}
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template