【发布时间】:2013-01-22 07:59:15
【问题描述】:
我目前正在尝试用 C# 制作一个 2D 连接四游戏。你知道什么时候所有 4 种颜色都必须匹配。无论如何,我目前正试图让碰撞工作,但不断收到错误消息“索引超出数组范围”你知道这是为什么吗?谢谢
private void rules()
{
int count = 0;
if (btn[maxR, maxC].BackColor == Color.Red)
{
count = 1;
}
for (int i = 0; i <= btn.Length; i++)
{
if (btn[maxR, i].BackColor == Color.Red)
{
count++;
}
}
if (count >= 4)
{
lbl2.Text = "winner";
}
}
【问题讨论】:
-
哪一行抛出
exception?您确实应该使用try/catch块并捕获exceptionobject的.Message属性。 -
您正试图访问一个负数或等于或大于数组长度的数的数组。例如,如果您声明一个大小为 [maxR] 的数组,则可用索引为 0、1、2...maxR-1(这是 maxR 个条目!),并且在 maxR 处访问将引发异常。跨度>