第五章第十九题(打印金字塔形的数字)(Display numbers in a pyramid pattern)

第五章第十九题(打印金字塔形的数字)(Display numbers in a pyramid pattern)

  • **5.19(打印金字塔形的数字)编写一个嵌套的for循环,打印下面的输出:
    在这里插入图片描述
    **5.19(Display numbers in a pyramid pattern) Write a nested for loop that prints the following output:
    在这里插入图片描述
  • 参考代码:
package chapter05;

public class Code_19 {
    public static void main(String[] args) {
        for(int lines = 8;lines >= 1;lines--)
        {
            for(int i = 1;i <= 4 * (lines-1);i++)
                System.out.print(" ");
            for(int i = 1;i <= (9 - lines);i++)
                System.out.printf("%4d",(int)Math.pow(2, i-1));
            for(int i = (8 - lines);i >= 1;i--)
                System.out.printf("%4d",(int)Math.pow(2, i-1));
            System.out.print("\n");
        }
    }
}

  • 结果显示:
                               1
                           1   2   1
                       1   2   4   2   1
                   1   2   4   8   4   2   1
               1   2   4   8  16   8   4   2   1
           1   2   4   8  16  32  16   8   4   2   1
       1   2   4   8  16  32  64  32  16   8   4   2   1
   1   2   4   8  16  32  64 128  64  32  16   8   4   2   1

Process finished with exit code 0

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值