我需要绘制一个数字菱形,例如高度为9:
1
222
33333
4444444
555555555
4444444
33333
222
1
我写了代码,我设法得到了相同的钻石,但有星星。我想要这些数字。我怎样才能做到这一点?以下是我到目前为止所做的事情:
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("a");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("b");
System.out.print("\n");
}
答案 0 :(得分:2)
关于您的代码:
System.out.print("\n");
应替换为System.out.println()
。"a"
和"b"
,而应打印循环索引并查看可从中获取的内容。这是@ Tsung-Ting Kuo解决方案。你可以用更少的循环来完成它,在我看来更容易理解。请考虑以下算法:
height
排除)height
除外)height/2-row-1
row-height/2
时
height/2 - min
min = Math.min(row+1, height-row)
时。height/2+row+1
height/2+height-row
时
height/2 + min
min = Math.min(row+1, height-row)
时。Math.min(row+1, height-row)
。转入代码:
public static void main(String[] args) {
int height = 9;
for (int row = 0; row < height; row++) {
for (int column = 0; column < height; column++) {
int min = Math.min(row+1, height-row);
if (column <= height / 2 - min || column >= height / 2 + min) {
System.out.print(" ");
} else {
System.out.print(min);
}
}
System.out.println();
}
}
示例输出:
1
222
33333
4444444
555555555
4444444
33333
222
1
答案 1 :(得分:0)
请尝试以下代码(我已测试过),只更改两个print
:
public static void main(String[] args) throws Exception {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print(i/2+1); // Change here
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print(i/2+1); // Change here
System.out.print("\n");
}
}
输出:
1
222
33333
4444444
555555555
4444444
33333
222
1
答案 2 :(得分:0)
您可以使用 IntStream
绘制数字菱形,如下所示:
int m = 5;
int n = 5;
String[][] arr = IntStream
.rangeClosed(-m, m)
.map(Math::abs)
.mapToObj(i -> IntStream
.rangeClosed(-n, n)
.map(Math::abs)
.mapToObj(j -> i + j > Math.max(m, n) ? " " : "" + (m - i))
.toArray(String[]::new))
.toArray(String[][]::new);
// formatted output
Arrays.stream(arr).map(row -> String.join(" ", row)).forEach(System.out::println);
0
1 1 1
2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4 4 4
3 3 3 3 3 3 3
2 2 2 2 2
1 1 1
0
另见:Filling a 2d array with numbers in a rhombus form • Print this diamond
答案 3 :(得分:0)
使用作为 Java-11 一部分引入的 String#repeat
,您可以使用单循环来实现。
public class Main {
public static void main(String[] args) {
final int MID_ROW_NUM = 5;
for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
int x = Math.abs(i);
System.out.println(" ".repeat(x) + String.valueOf(MID_ROW_NUM - x).repeat((MID_ROW_NUM - x) * 2 - 1));
}
}
}
输出:
1
222
33333
4444444
555555555
4444444
33333
222
1
您只需将空间量增加一个字符即可打印钻石的变体:
public class Main {
public static void main(String[] args) {
final int MID_ROW_NUM = 5;
for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
int x = Math.abs(i);
System.out.println(" ".repeat(x) + ((MID_ROW_NUM - x) + " ").repeat((MID_ROW_NUM - x) * 2 - 1));
}
}
}
输出:
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4
3 3 3 3 3
2 2 2
1