【发布时间】:2023-03-22 02:00:01
【问题描述】:
我一直在使用这个类的数组并且我一直在使用 foreach 但是当我尝试使用 for 循环(实际上是索引)访问它时,它会从数组中消失。有什么帮助吗?
-工作-
[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);
}
}
-不工作-
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 种 可以 被视为“满屋”