Java implements the print triangle method, and java implements the print triangle
The examples in this article share the code for printing the positive triangle in java for your reference. The specific content is as follows:
Code:
Package BasicType; /*** encapsulate a method that can print the positive triangle based on user input values * @ author Administrator */public class Enme {// n indicates the number of printed layers public static void print_positive_triangle (int n) {// The first layer is 1, the second layer is 3, and the third layer is 5... the n layer is the last * int last = 2 * (n-1) + 1; // controls the number of layers printed 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; // print the layer and wrap the System. out. println (""); // controls the style to be printed on the current layer. The square for (int j = 0; j <= last; j ++) is printed by default) {// If j is less or equal than the number of spaces to be filled or j is greater than the sum of the number of positions occupied by X and the number of spaces to be filled, 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 for your learning and support for helping customers.