【发布时间】:2011-03-16 06:18:29
【问题描述】:
在以下代码中,我收到了超出范围的异常。
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 值时,会发生 Out of ranged 异常。
这里是异常输出:
System.IndexOutOfRangeException 是 未处理
Message="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(窗体 调频) 在 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;
}
【问题讨论】:
-
这是您可以提供的产生错误的最小代码块吗?
-
那是一些丑陋的代码......必须有更好的方法来做你正在做的任何事情。 minValue 是在哪里定义的?
-
最小的代码块?是的,因为它是产生错误的唯一代码块,抱歉。 :-) 丑陋的代码,绝对真理!由于这个应用程序仍处于开发的早期阶段,我预计代码会很丑陋。但是由于这个应用程序是今天早些时候才启动的,所以清理代码仍然很麻烦......
标签: c#