public class Main {
    public static void main(String[] args) {
        int n = 4;
        for (int i = 1; i <= n; i++) { // 控制行数
            for (int j = 1; j <= i; j++) { // 打印左半部分的数字
                System.out.print(j + " ");
            }
            for (int j = i - 1; j >= 1; j--) { // 打印右半部分的数字
                System.out.print(j + " ");
            }
            System.out.println(); // 换行
        }
    }
}

输出结果:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 

原文地址: https://www.cveoy.top/t/topic/cw6y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录