IndexOutOfRangeException?

时间:2010-07-15 07:22:54

标签: 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());

}

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

这是异常输出:

  

System.IndexOutOfRangeException是   未处理
  消息= “IndexOutOfRangeException”
  堆栈跟踪:          在Table_Top_Game_Dice.Form1.btnRoll_Click(对象   发件人,EventArgs e)          在System.Windows.Forms.Control.OnClick(EventArgs   E)          在System.Windows.Forms.Button.OnClick(EventArgs   E)          在System.Windows.Forms.ButtonBase.WnProc(WM   wm,Int32 wParam,Int32 lParam)          在System.Windows.Forms.Control._InternalWnProc(WM   wm,Int32 wParam,Int32 lParam)          在Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr   hwnMain)          在System.Windows.Forms.Application.Run(Form   调频)          在Table_Top_Game_Dice.Program.Main()

这里的任何建议都将不胜感激。为了解决这个问题,我一直把头撞在墙上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;
    }

3 个答案:

答案 0 :(得分:5)

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

randomNumber()方法生成一个随机数组,其中数组的大小和最大值是独立的。因此,如果使用{ 1, 7, 13 } 3和arraySize 20进行调用,则可能会返回value

然后使用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循环?

答案 1 :(得分:0)

我没有发现错误,但你重复了很多代码。尝试将所有这些代码封装在函数内的每个“if(cbndXX.SelectedIndex!= YY)”中。

这样的事情:

private void RefactorizedFunction(ComboBox cmb, ComboBox cbGame, ref int success, ref int botch, int value, int maxsuxcess)
    {


            var arraySize = (int)cmb.SelectedIndex;
            int[] refArray = randomNumber(value, arraySize);
            foreach (int i in refArray)    //WARNING HERE...
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] >= maxsuxcess)
                    {
                        success++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch++;
                    }
                }
                if (cbGame.SelectedIndex != 2) continue;
                if (refArray[i] >= maxsuxcess)
                {
                    success++;
                }
                if (refArray[i] == 1)
                {
                    botch++;
                }
            }
    }

它没有经过测试,也无法解决您的问题,但我确保您的代码更容易调试。还有其他方法可以改进你的代码,比如使用dicctionary或数组而不是所有这些successXX和botchXX vars,但是......一步一步。

答案 2 :(得分:0)

这段代码是不是能够生成一个等于数组大小的int,然后在i中从数组中读取该项时会导致outofrange异常。

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

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