我一直在使用這個類的數組一段時間,我一直在使用foreach,但是當我嘗試訪問它使用for循環(實際索引)它走出數組。任何幫助?爲foreach正確工作時索引不正確
-Working-
[TestMethod]
public void Scorer_FullHouse_ReturningZero()
{
SetOfDice[] diceRolls = new SetOfDice[] {
new SetOfDice(new int[] {1,1,2,2,3}),
new SetOfDice(new int[] {3,5,3,4,3}),
new SetOfDice(new int[] {7,7,7,7,7})
};
foreach (SetOfDice roll in diceRolls)
{
int score = scorer.getScore(roll, category);
Assert.AreEqual(score, 0);
}
}
- 未Working-
System.IndexOutOfRangeException:索引陣列的邊界之外。
[TestMethod]
public void Scorer_ThreeOfAKind_Success()
{
SetOfDice[] diceRolls = new SetOfDice[] {
new SetOfDice(new int[] {1,2,1,4,1}),
new SetOfDice(new int[] {1,7,6,5,2}),
new SetOfDice(new int[] {8,8,8,8,8})
};
int[] expectedResults = new int[] {
9,
0,
40
};
for (int i = 0; i < expectedResults.Length; i++)
{
Assert.AreEqual(expectedResults[i], scorer.getScore(diceRolls[i], GameVariables.Category.ThreeOfAKind));
}
}
檢查堆棧跟蹤 - 你確定這個錯誤不是來自'scorer.getScore'嗎? –
附註:我不確定遊戲是什麼,但在Yahtzee中,有5種_can_可以被認爲是「滿屋子」 –