打印数字的倒金字塔

编程入门 行业动态 更新时间:2024-10-28 03:19:54
本文介绍了打印数字的倒金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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; } }

更多推荐

打印数字的倒金字塔

本文发布于:2023-08-07 07:00:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:金字塔   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!