This article has shared with you the specific code for Java to print the positive triangle for your reference. The specific content is as follows
Code:
package BasicType;/** * Encapsulate a method that can print positive triangles based on the value passed by the user* @author Administrator */public class Enme { //n represents the number of layers printed public static void print_positive_triangle(int n){ //1 in the first layer, three in the second layer, and five in the third layer... Analogy for exiting the nth layer is last * int last = 2*(n-1)+1; //Control how many layers to print for (int i=0;i<n;i++) { // Calculate the number of spaces to be filled on the left of each layer int full_left = last/2-i; //After printing one layer, you need to break the line System.out.println(""); //Control the style to be printed in this layer, and print the square by default for(int j=0;j<=last;j++){ //If j is less than the number of spaces to be filled or equal or j is greater than the sum of the number of positions occupied by the padded * and the number of positions filled with spaces, print the space if (j<=full_left||j>full_left+2*i+1){ System.out.print(" "); } else{ System.out.print("*"); } } } } public static void main(String[] args) { print_positive_triangle(5); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.