![](/img/trans.png)
[英]Print out seasons and three pointers side by side not one below the other in Java with Selenium. How can i do that?
[英]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 2 3
1 2 3 4
但這是我正在尋找的輸出
1
1 2
1 1 2 3
1 2 1 2 3 4
我不知道如何,感謝幫助和解釋,因為我也喜歡對其他類型的東西這樣做
要打印第一個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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.