问题及代码:
问题描述:
利用循环打印金字塔数
问题代码:
public class J {
public static void main(String[] args) {
int i, j, k;
for (i = 0; i < 8; i++) {
for (j = 0; j < 7 - i; j++) {
System.out.print(" ");
}
for (k = 0; k < 2 * i + 1; k++) {
if (k <= i) {
System.out.printf("%5d", (int) Math.pow(2, k));
} else {
System.out.printf("%5d", (int) Math.pow(2, 2 * i - k));
}
}
System.out.println();
}
}
}
运行结果: