1. Introduction
Split a rectangle into small triangles, and print the triangles with serial numbers 1, 2, and 3.
To print serial numbers 1, 2, 3, there must be one outer loop plus three inner loops.
- The outer loop is set to for (int i =1; i <= 5; i++)
- 1 The number of triangle columns gradually decreases, so the inner loop can be set to for (int j = 5;j >=i;j–)
- 2 The number of triangular columns gradually increases, so the inner loop can be set to for (int k = 1;k <=i;k++)
- 3 Similarly, the number of columns gradually increases, but the number of columns is one less than 2, so the inner loop can be set to for (int t = 1;t <i;t++)
2. Code
package com.zhuo.base.com.zhuo.base;
public class TestDemo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >=i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
for (int t = 1; t < i; t++) {
System.out.print("*");
}
System.out.println();
}
}
}
Three. Results display
*
***
*****
*******
*********
Process finished with exit code 0