-3

我正在尝试用 java 制作游戏,但遇到了一个看似简单的问题。我需要一些方法来使用循环打印出多个东西,但不是传统的方式。基本上我需要做的是:

代替:

 for(int i=0;i<5;i++)
 {
      e.get(i);
 }

我需要这样做:

for(int i=0;i<5;i++)
{
e.get(0);
e.get(1); //but 1 and above can only be there after a number has been increased past 0
e.get(2);
e.get(3);   
e.get(4);
}

在哪里改变我也会改变你有多少“e.get()”。

有任何想法吗?

把事情弄清楚:

这不起作用:

public static void main(String[] args)
{
    int l=5;
    for(int i=0;i<l;i++)
    {
        for(int o=0;o<l;o++)
        {
            e.get(o);
        }
    }
}

但类似的事情会:

public static void main(String[] args)
{
    e.get(0);
    e.get(1); //but 1 and above can only be there after a number has been increased past 0
    e.get(2);
    e.get(3);
    e.get(4);
}

我已经尝试过嵌套的 for 循环,但它不适用于我的程序。为了让我的程序正常工作,每个“e.get(0);” 需要亲自到场。

抱歉,如果我说不清楚,我已经连续编程了 6 个小时,并且正在接近一堵墙:/

4

4 回答 4

2
int num = 5;
for (int i=0; i<num; i++)
    for (int j=0; j<num; j++)
        e.get(j);

编辑:

你是这个意思吗?

int num = 5;
e.get(0);

for (int i=1; i<num; i++)
    for (int j=0; j<num; j++)
        e.get(j);
于 2012-05-24T23:55:49.957 回答
1

所以像这样的东西?

for (int i = 0; i < 5; i++) {
    e.get(0);
    if (i > 0)
        e.get(1); //but 1 and above can only be there after a number has been increased past 0
    if (i > 1)
        e.get(2);
    if (i > 2)
        e.get(3);
    if (i > 3)
        e.get(4);
}

这将确保所有 e.get() 调用都在您的代码中,并且我相信如果我了解您想要的内容(我可能不知道!)会正确调用它们。

于 2012-05-25T00:49:08.950 回答
1
for(int i = 0; i < 5; i++)
{
    for (int j = 0; j < i; j++)
    {
        e.get(j);
    }
}

像这样的东西?

于 2012-05-24T23:55:39.917 回答
1

我会尝试e.length;

for(int i=0;i<e.length;i++)
 {
      e.get(i);
 }
于 2012-05-24T23:57:08.787 回答