[英]How do I convert this while loop to a for loop in java?
給出:
int counter = 10;
while (counter > 0)
{
System.out.println(counter);
counter = counter - 1;
}
到目前為止,我有:
for (int i = 0, i < 10, i --){
int counter = 10
System.out.println(counter)
}
我不認為這是正確的,但我不太明白。 任何幫助都會很棒。 謝謝
for (int counter = 10; counter > 0; counter--) {
System.out.println(counter);
}
不需要創建兩個整數變量,只需在循環中使用計數器並打印即可。
循環包含三個部分:
對於while循環,這些是:
counter = 10
counter > 0
counter = counter - 1
對於for循環,我們具有for(初始化,測試,步驟){...}`
因此,將while循環部分替換為for循環,您將得到答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.