无法弄清楚为什么这会导致无限循环

时间:2012-07-01 17:04:02

标签: for-loop while-loop logic infinite-loop

所以我有这行代码,它不时地产生一个无限循环。我的逻辑在某处不正确吗? “if(randomNumbersForSelectionArray.Count> 0)”中的if语句应该始终返回true,但它不会返回true。然后,当我执行else语句并检查代码在进入无限循环时是否应该为true时,它确认我的逻辑应该是正确的。我似乎无法弄清楚这出错的地方。谢谢!


以下是我收到的一些示例输出。

12 | 2 =应= 2 这为什么会破裂!?!?!? 12 | 2 =应= 2 这为什么会破裂!?!?!? ...无限


int countLoop = 0;

if(trackFitnessRankArray.Length> = 1) {

            while (randomNumbersForSelectionArray.Count > 0)
            {
                countLoop++;

                for (int j = trackFitnessRankArray.Length - 1; j >= 0; j--)
                {
                    if (randomNumbersForSelectionArray.Count > 0)
                    {
                        if (randomNumbersForSelectionArray[0] >= (trackFitnessRankArray[j].CutoffPointForReproduction - trackFitnessRankArray[j].ChanceOfReproduction) && randomNumbersForSelectionArray[0] < trackFitnessRankArray[j].CutoffPointForReproduction)
                        {
                            //take the selected AIs and put them in an array
                            selectedToBreed.Add(trackFitnessRankArray[j]);

                            //remove the number from the randomNumber array
                            randomNumbersForSelectionArray.RemoveAt(0);
                        }
                        else
                        {
                            //if we're in an infinite loop
                            if (countLoop > AI_IN_EACH_GENERATION)
                            {
                                if (randomNumbersForSelectionArray[0] == trackFitnessRankArray[j].CutoffPointForReproduction)
                                {
                                    if (j != 0)
                                        Debug.WriteLine(j + "| " + randomNumbersForSelectionArray[0] + " =should= " + (trackFitnessRankArray[j - 1].CutoffPointForReproduction + trackFitnessRankArray[j - 1].ChanceOfReproduction));
                                    if (randomNumbersForSelectionArray[0] != (trackFitnessRankArray[j - 1].CutoffPointForReproduction + trackFitnessRankArray[j - 1].ChanceOfReproduction))
                                        Debug.WriteLine("Why is this breaking!?!?!?");
                                }
                            }
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

组件

//remove the number from the randomNumber array
                            randomNumbersForSelectionArray.RemoveAt(0);

应该是if