Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有数组的For语句_Java_Arrays_For Loop - Fatal编程技术网

Java 带有数组的For语句

Java 带有数组的For语句,java,arrays,for-loop,Java,Arrays,For Loop,我正在研究一种改变代码长度的方法 我有这个: rects[1].setLocation(0, 0); rects[2].setLocation(100, 0); rects[3].setLocation(200, 0); rects[4].setLocation(300, 0); rects[5].setLocation(400, 0); rects[6].setLocation(500, 0); rects[7].setLocation(

我正在研究一种改变代码长度的方法

我有这个:

    rects[1].setLocation(0, 0);
    rects[2].setLocation(100, 0);
    rects[3].setLocation(200, 0);
    rects[4].setLocation(300, 0);
    rects[5].setLocation(400, 0);
    rects[6].setLocation(500, 0);
    rects[7].setLocation(0, 50);
    rects[8].setLocation(100, 50);
    rects[9].setLocation(200, 50);
    rects[10].setLocation(300, 50);
    rects[11].setLocation(400, 50);
    rects[12].setLocation(500, 50);
    rects[13].setLocation(0, 100);
    rects[14].setLocation(100, 100);
    rects[15].setLocation(200, 100);
    rects[16].setLocation(300, 100);
    rects[17].setLocation(400, 100);
    rects[18].setLocation(500, 100);
    rects[19].setLocation(0, 150);
    rects[20].setLocation(100, 150);
    rects[21].setLocation(200, 150);
    rects[22].setLocation(300, 150);
    rects[23].setLocation(400, 150);
    rects[24].setLocation(500, 150);
我把它改成这样:

    for(int i = 1; i < 25; i++)
    {
        for(int j = 0; j < 550; j +=50)
        {
            for(int k = 0; k < 550; k +=50)
            {
                rects[i].setLocation(j, k);
            }
        }
    }

问题是后者不起作用,尽管它应该起作用。我的问题是,问题是什么?我试过很多方法来解决这个问题,但都不管用。我不得不用谷歌搜索这个问题,因为我不知道问题出在哪里。如果值得注意的话,这也是小程序的代码。

您正在执行最里面的语句24*10*10=2400次


您应该将其作为单个循环写入,并按顺序计算x和y值。

您的循环应该如下所示:

for (int i=0; i<24; i++) {
    int x = (i%6)*100;
    int y = (i/6)*50;
    //Array indexes start from 1, whereas this  
    //loop starts from 0, hence adjusting below
    rects[i+1].setLocation(x, y);
}
/**  
  *  Do two things every 6th iteration:
  *
  *    1.) Reset j to zero
  *    2.) Increment k by 50
  *
  *  Otherwise increment j by 100 every iteration.
  *
  */

for (int i = 1; i < 25; i ++) {
    if (isMultipleOfSix(i)) {
        j = 0;
        k += 50;
    }
    rects[i].setLocation(j, k);
    j += 100;
}

private boolean isMultipleOfSix(int num) {
    return ( num % 6 == 0 );
}
您不需要三个嵌套循环,因为您只分配给一个数组


顺便问一下,你的矩形数组索引不是应该从0开始吗?

我想你应该这样:

for (int i=0; i<24; i++) {
    int x = (i%6)*100;
    int y = (i/6)*50;
    //Array indexes start from 1, whereas this  
    //loop starts from 0, hence adjusting below
    rects[i+1].setLocation(x, y);
}
/**  
  *  Do two things every 6th iteration:
  *
  *    1.) Reset j to zero
  *    2.) Increment k by 50
  *
  *  Otherwise increment j by 100 every iteration.
  *
  */

for (int i = 1; i < 25; i ++) {
    if (isMultipleOfSix(i)) {
        j = 0;
        k += 50;
    }
    rects[i].setLocation(j, k);
    j += 100;
}

private boolean isMultipleOfSix(int num) {
    return ( num % 6 == 0 );
}
对于i的每一个值,你都在所有的j和k上循环,最终将所有的位置设置为500500

你应该做的是把j和k作为单独的变量放在循环外,也许把它们叫做x,y,并在每个循环中更新它们,例如

int x = 0;
int y = 0;
for(int i=0; i<25; i++) {
    rects[i].setLocation(x, y);
    if(x == 500) {
        x = 0;
        y += 50;
    } else {
        x += 100;
    }
}

如果对代码进行一点跟踪,您会发现代码执行以下操作:

rects[1].setLocation(0, 0);
rects[1].setLocation(0, 50);
rects[1].setLocation(0, 100);
rects[1].setLocation(0, 150);
...
这显然不是你想要的。您只需要设置总共24个值,因此只有一个循环。您可以使用模运算符获得适当的值

for(int i = 1; i < 25; i++)
{
    rects[i].setLocation(((i-1)%6)*100, ((i-1)/6)*50);
}
一些解释:


i-1/6可以工作的原因是这是整数除法。结果将被截断为整数。例如,11/6=1。

尝试以下方法:

for (int i = 0, y = 0; i <= 24; y += 50) {
  for (int x = 0; x <= 500; x += 100) {
    locs[++i].setLocation(x, y);
  }
}

而且,不,我不是一个优化编译器。

我发现您的代码中有几个问题可能会给您带来问题:

您的原始代码显示了 setLocation函数递增100,而for循环 显示递增50的整数。我相信你想要的就是原版。 您的整数i、j和k是以它们自己的方式创建的 局部范围。这意味着一旦你完成了你的“k” 循环,k从内存中删除。为了避免这种情况,您可以创建所有 您的变量位于原始for循环之外。 您不需要所有这些嵌套for循环 下面是我要做的:

for(int i = 1, j = 0, k = 0; i < 24; j += 100) 
{
  if(j > 500)
  {
    j = 0;
  }

  if(i%6 == 0)
  {
    k += 50;
  }

  rects[i].setLocation(j, k);
}

循环变量i、j和k没有按应有的方式递增。稍后将发布答案…您正在循环24*11*11次,而您只需循环24次。这在算法上是正确的。如果能用一种可读性更好的形式写就好了。。。对于一个新手程序员来说,@StephenC说得很好。相应地重构。你认为它需要更多的编辑吗?谢谢你,我相信它会有用的。我从来没有意识到你可以只做一个for循环,然后从中提取变量。非常感谢。