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

时间:2022-03-01 21:11:33

So I have this line of code where it keeps generating an infinite loop every now and then. Is my logic incorrect somewhere? The if statement inside of "if (randomNumbersForSelectionArray.Count > 0)" should always return true, but it doesn't. Then when I do an else statement and check to see if the code should be true when it goes into an infinite loop, it confirms that my logic should be correct. I can't seem to figure out where this is going wrong. Thanks!!

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


Here is some sample output that I'm getting.

这是我得到的一些示例输出。

12| 2 =should= 2
Why is this breaking!?!?!?
12| 2 =should= 2
Why is this breaking!?!?!?
...for infinite

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


int countLoop = 0;

int countLoop = 0;

if (trackFitnessRankArray.Length >= 1) {

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 个解决方案

#1


0  

The component

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

should be out of the if

应该是if

#1


0  

The component

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

should be out of the if

应该是if