欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

C语言实验——打印金字塔_JAVA

发布时间:2025/3/21 编程问答 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C语言实验——打印金字塔_JAVA 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

输入n值,打印下列形状的金字塔,其中n代表金字塔的层数。

Input
输入只有一个正整数n。
Output
打印金字塔图形,其中每个数字之间有一个空格。
Sample
Input
3
Output
1
1 2 1
1 2 3 2 1

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner reader = new Scanner(System.in);int n;n = reader.nextInt();for(int i = 1; i <= n; i++) {for(int j = 1; j <= 2 * (n - i); j++) {System.out.print(" ");}for(int j = 1; j <= i; j++) {if(i == 1) System.out.print(j);else System.out.print(j+" ");}for(int j = i - 1; j >= 1; j--) {if(j == 1) System.out.print(j);else System.out.print(j+" ");}System.out.println();}reader.close();} }

总结

以上是生活随笔为你收集整理的C语言实验——打印金字塔_JAVA的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。