java打印金字塔数字
时间: 2023-10-20 22:15:51 浏览: 109
您好!欢迎与我对话。下面是Java打印金字塔数字的示例代码:
```
import java.util.Scanner;
public class PyramidNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入金字塔的层数:");
int n = input.nextInt();
input.close();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--) {
System.out.print(k + " ");
}
for (int m = 2; m <= i; m++) {
System.out.print(m + " ");
}
System.out.println();
}
}
}
```
您可以根据自己的需求调整金字塔的层数。希望对您有帮助!
阅读全文