Java:改进打印数字金字塔,具有对齐和反转功能
注 1:我已经有了蛮力、冗长、循环混乱的代码。
注 2:寻找更优雅的方式来建立这一点 - “更少”的行数和巧妙的逻辑。 注3:不是作业,我是一个试图提高的业余爱好者。
要编码的函数:
printNumericPyramid(int depth, String alignment, String orientation);
深度:+ve 最大 4 的整数
对齐方式:“左”/“右”之一
方向:“直立”/“倒立”之一
0 始终位于顶点,无论是直立还是倒立
0259/9520 永远是金字塔的高度。
示例调用和预期输出:
printNumericPyramid(4, "left", "inverted");
9876
543.
21..
0...
printNumericPyramid(4, "right", "upright");
...0
..12
.345
6789
printNumericPyramid(4, "right", "inverted");
6789
.345
..12
...0
printNumericPyramid(4, "left", "upright");
0...
21..
543.
9876
如果“较少”的行数可以成为所提议解决方案的重点,那就太好了。不是通过删除所有换行符:),而是通过最小化代码(即使它会损害可读性)。
Note 1: I already have the brute force, verbose, loopy messy code in place.
Note 2: Looking for more elegant ways of establishing this - "lesser" number of lines and clever logic.
Note 3: Not homework, I am a hobbyist trying to improve.
Function to be coded:
printNumericPyramid(int depth, String alignment, String orientation);
depth: +ve integer up to 4
alignment: one of "left"/"right"
orientation: one of "upright"/"inverted"
0 will always be at the apex, whether upright or inverted
0259/9520 will always be the height of the pyramid.
Samples calls and expected outputs:
printNumericPyramid(4, "left", "inverted");
9876
543.
21..
0...
printNumericPyramid(4, "right", "upright");
...0
..12
.345
6789
printNumericPyramid(4, "right", "inverted");
6789
.345
..12
...0
printNumericPyramid(4, "left", "upright");
0...
21..
543.
9876
It would be great if "lesser" number of lines could be the focus of the proposed solution. Not by removing all linebreaks:), but by minimization of code (even if it hurts readibility).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(首先请注意,您的方法定义很容易出错:如果参数只能采用两个值,您可以使用布尔值而不是字符串。如果您拼写错误“左”或“右”或“倒置”或“直立” ",你的方法可以工作,但会产生错误的结果)
如果你想专注于少量的行,那么你不应该重复自己,你可以使用三元运算符和可用的 Java API。
您可以基于以下类型的循环中非常“智能”且完全不可读的代码编写一个非常简短的解决方案:
然而,这最终可能不会那么容易阅读,而且很容易得到逻辑错误。
所以我写了另一个解决方案:
在每个字符处,您要么打印下一个值(从 0 到 9),要么打印一个点“.”。
当一行“完成”时,我要么添加该行,要么反转该行,具体取决于alignment参数的值。
如果要求反转表示,则只需反转集合即可
(First note that your method definition is error-prone: if an argument can only take two values you can use a boolean instead of a String. If you mispell "left" or "right" or "inverted" or "upright", your method will work yet produce the wrong result)
If you want to focus on a low number of lines then you should not repeat yourself and you can make use of the ternary operator and of the available Java APIs.
You could write a very short solution based on very "smart" and totally unreadable code inside the following kind of loop:
However this may not end up being that easy to read and it's going to be easy to get the logic wrong.
So I wrote another solution:
at each character you either print the next value (from 0 to 9) or you print a dot '.'.
when a line is "done", I either add that line or that line reversed, depending on the value of the alignment parameter.
if the inversed representation is asked, you can simply reverse the collection