打印数字的倒金字塔 [英] Print reverse pyramid of numbers

查看:69
本文介绍了打印数字的倒金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够像这样打印金字塔:

I am able to print pyramid like this :

   1
  123
 12345
1234567

我用来打印上面的数字金字塔的代码是:

Code i used to print the pyramid of numbers like above is :

                int a = 1;
                int b = 4;
                for (int i = 1 ; i <= 4 ; i++){
                    for (int c = 1 ; c <= b - 1 ; c++){
                        text.append("  ");
                    }
                    for (int k = 1 ; k <= a ; k++){
                        String result = String.valueOf(k);
                        text.append(result);
                    }
                    a = a + 2;
                    b--;
                    text.append("\n");
                }

但是,我面临的问题是我必须打印相同的金字塔,但是以相反的顺序打印:

but, the issue i am facing is i have to print the same pyramid, but in reverse order like this :

 1234567
  12345
   123
    1

有什么帮助吗?

推荐答案

尝试一下...

 for (int i = 7; i > 0; i--) {
        for (int j = 1; j <= i; j++) {
            System.out.print(j);
        }
        System.out.print("\n");

        if (i % 2 != 0) {
            i = i - 1;
        }
    }

这篇关于打印数字的倒金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆