需要指出的是:
-
你的方法可能无效。
-
System.out.print("\n");
=
System.out.println();
-
您正在修改
limit
以下是我的尝试:
public static void trianglePatterns(final int limit) {
// Triangle One
for (int x = 1; x <= limit; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + " ");
}
System.out.println();
}
System.out.println();
// Triangle Two
for (int x = limit; x > 0; x--) {
for (int y = 1; y <= x; y++) {
System.out.print(y + " ");
}
System.out.println();
}
System.out.println();
// Triangle Three
// first printint spaces of each row and then the numbers
for (int x = 1; x <= limit; x++) {
for (int space = (limit - x); space > 0; space--) {
System.out.print(" ");
}
for (int num = x; num > 0; num--) {
System.out.print(num + " ");
}
System.out.println();
}
System.out.println();
// Triangle Four
for (int x = limit; x >= 0; x--) {
for (int space = (limit - x); space > 0; space--) {
System.out.print(" ");
}
for (int num = 1; num <= x; num++) {
System.out.print(num + " ");
}
System.out.println();
}
}
的输出
trianglePatterns(5)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1