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