java realizes the method of printing positive triangle

  • 2021-01-06 00:37:12
  • OfStack

This article example for everyone to share java print positive 3 Angle specific code, for your reference, the specific content is as follows

Code:


package BasicType;
/**
 *  encapsulation 1 You can print a positive based on the value passed in by the user 3 Method of Angle 
 * @author Administrator
 */

public class Enme {
  //n Represents the number of layers to print 
  public static void print_positive_triangle(int n){
    // The first 1 layer 1 A, the first 2 layer 3 A, the first 3 layer 5 a ... Analog exit control n Layer is last a *
    int last = 2*(n-1)+1;
    // Controls how many layers to print 
    for (int i=0;i<n;i++)
    {  // Calculate the per 1 Number of Spaces to fill the left side of the layer 
      int full_left = last/2-i;
      // Print out 1 A newline is needed after the layer 
      System.out.println("");
      // Controls the style to be printed on this layer. Print square by default 
      for(int j=0;j<=last;j++){
        // if j Fewer or equal or than the number of Spaces to fill j overfilled * The space is printed by adding the number of Spaces occupied to the number of Spaces filled 
        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);  
}
}

Related articles: