0

下面的代码会导致无限循环吗?

  x=0;
  y=0;

  while(x<100)
  {
    minnum=100;
    maxnum=100;
    while(y<150)
    {
    if(random[x][y]<minnum)
      {
      minnum=random[x][y];
      minX=x;
      minY=y;
      y++;
      }
    else if(random[x][y]>maxnum)
      {
      maxnum=random[x][y];
      maxX=y;
      maxY=y;
      y++;
      }
    }
    x++;
    y=0;
  }  
4

11 回答 11

9

If both random[x][y]<minnum and random[x][y]>maxnum are false, you'll never exit out of the inner loop. More specifically if random[x][y] == 100 you're toast the first time through.

As a related question, what behavior do you see when you debug the source you've posted?

于 2009-09-14T18:00:26.347 回答
2

原来的问题有很多很好的答案,但即便如此,它们都不起作用!那是因为在第二个“if”块中有一个尽职尽责的错字——应该读

maxX = x; // not y!
maxY = y;
于 2009-09-14T20:24:29.207 回答
1

每当您的数组值介于 minnum 和 maxnum 之间时,这将进入一个无限循环。最初,这只是 100。第二遍是您看到的最小值和最大值之间的差异。例如,如果值为 3000,则 minnum 为 100,maxnum 为 3000。现在,100 到 3000(含)之间的任何数字都将导致无限循环。有关如何修复代码,请参阅其他答案。

雅各布

于 2009-09-14T21:44:37.827 回答
1

If neither of the if and else if in the inner loop is satisfied, you never increment y, thus making that inner loop undending. Looks like the y++ should be OUTSIDE of any conditional statements.

于 2009-09-14T18:00:53.083 回答
0

不知道现在的大学教授是不是应该知道学生可以这样上网得到答案?

无论如何,就像每个人都说的那样,如果 random[x][y]==100,那么这将始终在内部循环中。

于 2009-09-14T19:01:48.610 回答
0

On the inner while loop, if random[x][y] == minnum, then y will never be incremented and the program will go into an infinite loop.

于 2009-09-14T18:00:48.413 回答
0

If minnum <= random[x][y] <= maxnum then y is never incremented in the inner loop. Probably you should increment y in any case, even if none of the if-conditions are met. Best move the y++ outside of the ifs before the closing brace of the while loop.

You could also use for-loops to iterate over the possible values for x and y, which would reduce the probability of such mistakes.

于 2009-09-14T18:01:02.303 回答
0

If random[x][y] is ever exactly 100, this will loop infinitely, since it never does anything in that case.

于 2009-09-14T18:01:04.170 回答
0

这是我对这个周期的看法——关于如何编写“好”代码的一些担忧(从我的角度来看——我真的不是编程方面的大师)——不是必须遵循它们,但我认为它们还不错.

  // It's a good rule - to initialize minimum and maximum with one
  // (any) element from the array you will be working with
  minnum=random[0][0];
  maxnum=random[0][0];

  // This is my hamble opinion but I'll prefer to have variables for array sizes
  sizeX=100;
  sizeY=150;

  // Cannot you use for instead of while?
  for(x = 0; x < sizeX; ++x)
      for(y = 0; y < sizeY; ++y)
      {
          if(random[x][y] < minnum)
          {
              minnum = random[x][y];
              minX = x;
              minY = y;
          }
          else if(random[x][y] > maxnum)
          {
              maxnum = random[x][y];
              maxX = x;
              maxY = y;
          }
    }
于 2009-09-14T18:13:25.757 回答
0

问题是您仅在 random 小于 minnum 或大于 maxnum 时才提升 y,从而产生了两者都不发生的问题。

但是,为什么只有在这些条件发生时才推广它呢?无论这些条件如何,您都希望 y 得到提升。

您可能希望更改为:

while(y<150)
{
if(random[x][y]<minnum)
  {
  minnum=random[x][y];
  minX=x;
  minY=y;
  }
else if(random[x][y]>maxnum)
  {
  maxnum=random[x][y];
  maxX=y;
  maxY=y;
  }
  y++;
}

这样 y 和 x 总是被提升。

于 2009-09-14T18:13:29.143 回答
0

如果你用for循环替换你的while循环,你的代码会更有意义,更不容易出错,这将解决你的无限循环问题:

minnum=100;
maxnum=100;
for (int x = 0; x<100; x++)
{
  for(int y=0; y<150; y++)
  {
    if(random[x][y]<minnum)
    {
      minnum=random[x][y];
      minX=x;
      minY=y;
    }
    else if(random[x][y]>maxnum)
    {
      maxnum=random[x][y];
      maxX=y;
      maxY=y;
    }
  }
}
于 2009-09-14T18:17:08.757 回答