我用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你没有说你想要多少遍,所以我假设 5。如果你想要更少,可能会改变循环的上限(外层)。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);
}
尝试这个。
for (int x = 0,y = 0; x < 100; x++,y++) {
example.print(x + y); // You will need to tweak these values
}
100 在这里是一个假设值,因为循环将迭代的次数。
这是我的看法,*考虑到您发送给 print 方法的参数是您希望它打印的内容
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);
}
}
任何人都猜到你所说的“身体在那里”是什么意思,但我会试一试:
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);
}