2012-07-01 37 views
-2

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


这里是我得到一些样本输出。

12 | 2 =应= 2
这是为什么这样打破!?!?!?
12 | 2 =应= 2
这是为什么这样打破!?!?!?
...无限


INT countLoop = 0;

如果(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!?!?!?"); 
           } 
          } 
         } 
        } 
       } 
      } 
     } 

回答

0

组件

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

应该走出如果

相关问题