五、打印三角形
package com.zx.scanner; /** 打印n层三角形 **/ public class Demo05 { public static void main(String[] args) { //层数 int n = 5; for (int i = 1; i <= n; i++) { //填充:5 4 3 2 1个空格 for (int j = n; j >= i; j--) { System.out.print(" "); } //填充:1 2 3 4 5个* for (int j = 1; j <= i; j++) { System.out.print("*"); } //填充:0 1 2 3 4个* for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); } } }
结果如下: