IndexOutOfRangeException?

0 c#

在下面的代码中,我正在接收和超出范围异常.

private void btnRoll_Click(object sender, EventArgs e)
    {
        int success4 = 0;
        int success6 = 0;
        int success8 = 0;
        int success10 = 0;
        int success20 = 0;
        int botch4 = 0;
        int botch6 = 0;
        int botch8 = 0;
        int botch10 = 0;
        int botch20 = 0;

        if (cbnd4.SelectedIndex != 0)
        {

            int value = 4;
            int arraySize = (int)cbnd4.SelectedIndex;
            int[] refArray = randomNumber(value, arraySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] >= 2)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch4++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if(refArray[i] >= 2)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch4++;
                    }
                }


            }
        }
           /* if (cbmd4.SelectedIndex != 0)
            {
            }
            */
         if (cbnd6.SelectedIndex != 0)
        {
            int value = 6;
            int arrySize = (int)cbnd6.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {


                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] >= 4)
                    {
                        success6++;
                    } if (refArray[i] == 1)
                    {
                        botch6++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] >= 4)
                    {
                        success6++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch6++;
                    }
                }


            }
        }
        if (cbnd8.SelectedIndex != 0)
        {
            int value = 8;
            int arrySize = (int)cbnd8.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] >= 5)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch8++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] >= 5)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch8++;
                    }
                }


            }
        }
        if (cbnd10.SelectedIndex != 0)
        {
            int value = 10;
            int arrySize = (int)cbnd10.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] >= 7)
                    {
                        success10++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch10++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] >= 7)
                    {
                        success10++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch10++;
                    }
                }


            }
        }
        if (cbnd20.SelectedIndex != 0)
        {
            int value = 20;
            int arrySize = (int)cbnd20.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] >= 16)
                    {
                        success20++;
                    }
                    if (refArray[i] == 1)
                    {
                    botch20++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] >= 7)
                    {
                        success20++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch20++;
                    }
                }


            }
        }
        lBotch_Result.Text = Convert.ToString(botch4 + botch6 + botch8 + botch10 + botch20);
        lSuccess_Result.Text = Convert.ToString(success4 + success6 + success8 + success10 + success20);
        MessageBox.Show("d4 successes: " +
            success4.ToString() +
            "\r\nd6 Successes: " +
            success6.ToString() +
            "\r\nd8 Successes: " +
            success8.ToString() +
            "\r\nd10 Successes: " +
            success10.ToString() +
            "\r\nd20 Successes: " +
            success20.ToString() +
            "\r\nd4 Botches: " +
            botch4.ToString() +
            "\r\nd6 Botches: " +
            botch6.ToString() +
            "\r\nd8 Botches: " +
            botch8.ToString() +
            "\r\nd10 Botches: " +
            botch10.ToString() +
            "\r\nd20 Botches: " +
            botch20.ToString());

}
Run Code Online (Sandbox Code Playgroud)

当if(refArray [i]> = 7)且refArray.Length包含奇数int值时,会发生超出范围的异常.

这是异常输出:

System.IndexOutOfRangeException未处理
Message ="IndexOutOfRangeException"
StackTrace:在System.Windows.Forms.Button.OnClick上的System.Windows.Forms.Control.OnClick(EventArgs e)的Table_Top_Game_Dice.Form1.btnRoll_Click(Object sender,EventArgs e)处( EventArgs e)位于Microsoft.AGL.Forms的System.Windows.Forms.Control._InternalWnProc(WM wm,Int32 wParam,Int32 lParam)的System.Windows.Forms.ButtonBase.WnProc(WM wm,Int32 wParam,Int32 lParam).在Table_Top_Game_Dice.Program.Main()的System.Windows.Forms.Application.Run(Form fm)中的EVL.EnterMainLoop(IntPtr hwnMain)

这里的任何建议将不胜感激.为了解决这个问题,我一直把头撞在墙上5个小时.

哦,refArray从以下函数中获取值:(如果有帮助)

private int[] randomNumber(int value, int arraySize)
    {
        int[] randArray = new int[arraySize];
        maxValue = value;
        Random rand = new Random();
        for (int i = 0; i < arraySize; i++)
        {
           randArray[i] = rand.Next(minValue, maxValue);
        }
        return randArray;
    }
Run Code Online (Sandbox Code Playgroud)

Dan*_*ner 5

您显然是在尝试访问数组末尾之外的数组元素.

randomNumber()方法生成随机数组,其中数组的大小和最大值是独立的.因此,{ 1, 7, 13 }如果使用arraySize3和value20 调用它可能会返回.

然后使用迭代遍历数组foreach (int i in refArray).因此,将有三次迭代,i设置为1,然后是7,最后是13.

因此,如果使用您访问数组refArray[i],则尝试访问数组元素和索引1,7和13,因此IndexOutOfRangeException在第二次迭代中获取,因为您尝试访问索引7处的元素,而数组只包含3个元素.

你打算for (int i = 0; i < refArray.Length; i++)而不是foreach循环吗?