「Java案例」打印数字金字塔

案例解析

数字金字塔打印

编写程序,输入金字塔图案的行数,打印如下图所示的金字塔。
例如,输入行数3时,

        1   
    1   2   1   
1   2   4   2   1   

实现代码

# 源文件保存为“NumberPyramid.java”
import java.util.Scanner;

public class NumberPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入金字塔的行数:");
        int rows = scanner.nextInt();
        
        for (int i = 1; i <= rows; i++) {
            // 打印前导空格
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("    "); // 4个空格保持对齐
            }
            
            // 打印左半边数字(1,2,4,8...)
            for (int k = 1; k <= i; k++) {
                System.out.printf("%-4d", (int)Math.pow(2, k-1));
            }
            
            // 打印右半边数字(...4,2,1)
            for (int l = i-1; l >= 1; l--) {
                System.out.printf("%-4d", (int)Math.pow(2, l-1));
            }
            
            System.out.println(); // 换行
        }
        scanner.close();
    }
}

代码解析:

  • 输入处理:使用Scanner获取用户输入的金字塔行数
  • 外层循环:控制金字塔的行数(从1到rows)
  • 前导空格:每行前导空格数为总行数减当前行数,保持金字塔形状
  • 左半边数字:数字按2的幂次递增(1, 2, 4, 8…)
  • 右半边数字:数字按2的幂次递减(…4, 2, 1)
  • 格式化输出:使用printf和%-4d保持数字对齐,每个数字占4个字符宽度

运行示例(输入3):

        1    
    1   2   1    
1   2   4   2   1    

字母金字塔

# 源文件保存为“AlphabetPyramid.java”
public class AlphabetPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入金字塔的行数:");
        int rows = scanner.nextInt();
        
        for (int i = 1; i <= rows; i++) {
            // 打印前导空格
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("  "); // 2个空格
            }
            
            // 打印左半边字母(A,B,C...)
            for (char k = 'A'; k < 'A' + i; k++) {
                System.out.print(k + " ");
            }
            
            // 打印右半边字母(...B,A)
            for (char l = (char)('A' + i - 2); l >= 'A'; l--) {
                System.out.print(l + " ");
            }
            
            System.out.println();
        }
        scanner.close();
    }
}

运行结果

请输入金字塔的行数:3
    A 
  A B A 
A B C B A 

代码特点:

  • 打印字母金字塔而非数字
  • 使用字符运算生成字母序列
  • 更简单的空格处理(2个空格)
  • 输出示例(行数=3):

乘法表金字塔

# 源文件保存为“MultiplicationPyramid.java”
public class MultiplicationPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入金字塔的行数:");
        int rows = scanner.nextInt();
        
        for (int i = 1; i <= rows; i++) {
            // 打印前导空格
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("    ");
            }
            
            // 打印左半边
            for (int k = 1; k <= i; k++) {
                System.out.printf("%-4d", k * i);
            }
            
            // 打印右半边
            for (int l = i-1; l >= 1; l--) {
                System.out.printf("%-4d", l * i);
            }
            
            System.out.println();
        }
        scanner.close();
    }
}

运行结果

请输入金字塔的行数:3
        1   
    2   4   2   
3   6   9   6   3  

代码特点:

  • 打印乘法表形式的金字塔
  • 每行数字是该行号与列号的乘积
  • 输出示例(行数=3):

操作练习题

斐波那契金字塔

要求:

  • 将数字规律改为斐波那契数列
  • 第一行:1
  • 第二行:1 1 1
  • 第三行:1 1 2 1 1
  • 第四行:1 1 2 3 2 1 1

参考代码:

# 源文件保存为“FibonacciPyramid.java”
public class FibonacciPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入金字塔的行数:");
        int rows = scanner.nextInt();
        
        for (int i = 1; i <= rows; i++) {
            // 打印前导空格
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("    ");
            }
            
            // 生成斐波那契数列
            int[] fib = new int[i];
            fib[0] = 1;
            if (i > 1) fib[1] = 1;
            for (int k = 2; k < i; k++) {
                fib[k] = fib[k-1] + fib[k-2];
            }
            
            // 打印左半边
            for (int k = 0; k < i; k++) {
                System.out.printf("%-4d", fib[k]);
            }
            
            // 打印右半边
            for (int l = i-2; l >= 0; l--) {
                System.out.printf("%-4d", fib[l]);
            }
            
            System.out.println();
        }
        scanner.close();
    }
}

运行结果

请输入金字塔的行数:4
            1   
        1   1   1   
    1   1   2   1   1   
1   1   2   3   2   1   1 

空心数字金字塔

要求:

  • 只打印金字塔的边框数字
  • 内部用空格填充
  • 例如(行数=3):
        1   
    1   2   1   
1   2   4   2   1

参考代码:

# 源文件保存为“HollowPyramid.java”
public class HollowPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入金字塔的行数:");
        int rows = scanner.nextInt();
        
        for (int i = 1; i <= rows; i++) {
            // 打印前导空格
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("    ");
            }
            
            // 打印左半边
            for (int k = 1; k <= i; k++) {
                if (k == 1 || k == i || i == rows) {
                    System.out.printf("%-4d", (int)Math.pow(2, k-1));
                } else {
                    System.out.print("    ");
                }
            }
            
            // 打印右半边
            for (int l = i-1; l >= 1; l--) {
                if (l == 1 || l == i || i == rows) {
                    System.out.printf("%-4d", (int)Math.pow(2, l-1));
                } else {
                    System.out.print("    ");
                }
            }
            
            System.out.println();
        }
        scanner.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜晚回家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值