英文:
How do i put the other java number triangle side by side
问题
以下是翻译好的内容:
public class StarsAndDraws {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
for (int j = 4; j >= 1; j--){
if (j > i){
System.out.print(" ");
} else {
System.out.print(i - j + 1);
}
}
System.out.println();
}
}
}
期望的输出如下:
1
1 2
1 1 2 3
1 2 1 2 3 4
我不知道如何得到这个输出,帮助和解释将会很有帮助,因为我也想用类似的方法处理其他类型的内容。
英文:
So i was trying this code i found on the internet that let me make number triangles, the codes are like so
public class StarsAndDraws {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
for (int j = 4; j >= 1; j--){
if (j > i){
System.out.print(" ");
} else {
System.out.print(i - j + 1);
}
}
System.out.println();
}
}
}
the output looks like this
1
1 2
1 2 3
1 2 3 4
but this is the output im looking for
1
1 2
1 1 2 3
1 2 1 2 3 4
i have no idea how, help and explanation is appreciated because id like to do this to other kinds of stuff aswell
答案1
得分: -1
为了打印第一个 1/ 1 2
,需要为它定义另一个循环。
class Main {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
if (i < 3) {
System.out.print(" ".repeat(9));
} else {
System.out.print(" ".repeat((4 - i) * 2));
for (int j = 1; j <= i - 2; j++) {
System.out.print(j);
System.out.print(" ");
}
System.out.print(" ".repeat(6));
}
System.out.print(" ".repeat(2 * (4 - i)));
for (int j = 1; j <= i; j++) {
System.out.print(j);
System.out.print(" ");
}
System.out.println();
}
}
}
英文:
To print first 1/ 1 2
, it is needed to define another loop for that one too.
class Main {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
if (i < 3) {
System.out.print(" ".repeat(9));
} else {
System.out.print(" ".repeat((4 - i) * 2));
for (int j = 1; j <= i - 2; j ++) {
System.out.print(j);
System.out.print(" ");
}
System.out.print(" ".repeat(6));
}
System.out.print(" ".repeat(2 * (4 - i)));
for (int j = 1; j <= i; j ++) {
System.out.print(j);
System.out.print(" ");
}
System.out.println();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论