java使用 for 语句打印显示下列数字形式:n=4加注释11 2 11 2 3 2 11 2 3 4 3 2 1
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 著作权归作者所有。请勿转载和采集!