Home >Java >javaTutorial >How to create 6 triangles using java

How to create 6 triangles using java

(*-*)浩
(*-*)浩Original
2019-05-22 14:33:183443browse

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

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!

Statement:
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
Previous article:What is class in java?Next article:What is class in java?