非常簡單,但我似乎無法弄清楚,我只需要將while循環轉換爲do-while。目標是打印1-9和9-1,例如「1,22,333,4444」等。謝謝你,如果你幫我!雖然做while循環的同時做
int y = 1;
int x = 1;
while (x < 10) {
y = 1;
while (y <= x) {
y++;
System.out.print(x + "");
}
System.out.println();
x++;
}
int a = 9;
int b = 1;
while (a >=1) {
b = 1;
while (b<= a) {
b++;
System.out.print(a + "");
}
System.out.println();
a--;
}
我的嘗試打印1-9,但作爲單個數字和無限運行,但在第一次9後不打印任何東西。
int c = 1;
int d =1;
do { System.out.print(c +"");
c++;
System.out.println();
}
while (c<10);{
y=1;
while (d<=c);
}
1)爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 2)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 3)源代碼中的單個空白行是* always *就足夠了。 '{'之後或'}'之前的空行通常也是多餘的。 –
您錯過了第二個「do」聲明。第二個while語句實際上是一個while循環,它不執行任何代碼,因爲變量的內容沒有改變,它等價於「while(true);」 (也似乎並沒有改變d的值,y也沒有定義) –
分號太多 – pamphlet