[英]java: is there a loop that can do this?
我正在用java做游戲,需要能夠做一個像這樣的循環:
第一次通過循環:
for(int i=0;i<5;i++)
{
example.print(0);
}
第二遍:
for(int i=0;i<5;i++)
{
example.print(0);
example.print(1);
}
等等,每次都添加另一個example.print()。
為了使程序正常工作,每個“ example.print()”必須實際存在代碼。 有任何想法嗎?
聽起來像您想要嵌套循環:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
example.print(i + j); // This will need adjusting
}
}
筆記:
i + j
部分,以獲得所需的輸出。 您沒有說當我們超過前五個時會發生什么。 :-) i
循環(外部循環)的上限。 int loopCounter = 0;
for(int i=0;i<5;i++)
{
for(int k=0; k<loopCounter; k++)example.print(k);
loopCounter++;
}
我的版本:
for(int i=0;i<5;i++)
{
for(int k=0; k<i; k++)
example.print(k);
}
有人猜測您在“物理上在那兒”的意思,但是我給您一個鏡頭:
final Example example = new Example();
for (int i = 0; i < 5; i++)
switch (i) {
case 4: example.print(i-4);
case 3: example.print(i-3);
case 2: example.print(i-2);
case 1: example.print(i-1);
case 0: example.print(i-0);
}
嘗試這個。
for (int x = 0,y = 0; x < 100; x++,y++) {
example.print(x + y); // You will need to tweak these values
}
100是此處的假定值,循環的次數不計其數。
這是我的看法,*考慮發送給打印方法的參數是您希望打印的參數
int n=3; //n is the highest param value you want your print method to receive,
//here it's just 3
for (int i=0; i<n; i++) {
for (int j=0; j<(i+1)*5; j++) {
example.print(j/5);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.