在下面的代码中,我正在接收和超出范围异常。
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(refArrayi >= 7)和refArray.Length包含一个奇数int值时,就会出现超出范围的异常。
下面是异常输出:
System.IndexOutOfRangeException未被处理
Message="IndexOutOfRangeException“
Table_Top_Game_Dice.Form1.btnRoll_Click(Object StackTrace:在System.Windows.Forms.Control._InternalWnProc(WM wm,Int32 wParam,EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.ButtonBase.WnProc(WM wm,Int32 wParam,Int32 lParam)Int32 lParam) at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) at System.Windows.Forms.Application.Run(Form fm) at 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;
}发布于 2010-07-15 07:42:17
显然,您正在尝试访问数组结束后的数组元素。
randomNumber()方法生成一个随机数数组,其中数组的大小和最大值是独立的。因此,如果与{ 1, 7, 13 } 3和value 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循环?
发布于 2010-07-15 07:42:21
我没有发现错误,但是您正在重复大量的代码。尝试将所有这些代码封装在函数中的每个"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++;
}
}
}它没有经过测试,也不会解决您的问题,但我相信您的代码将更易于调试。还有其他改进代码的方法,比如使用字典或数组来代替所有这些successXX和botchXX vars,但是.循序渐进。
发布于 2010-07-15 07:43:04
这段代码难道不能生成一个等于数组大小的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++;
}
}https://stackoverflow.com/questions/3253314
复制相似问题