我刚刚遇到一个很常见的问题,但我不确定为什么会在这种情况下发生。
string s;
int c1, c2, c3, c4;
private void button2_Click(object sender, EventArgs e)
{
String number;
s = textBox1.Text;
int[] d = s.Select(c => (int)c - (int)'0').ToArray();
try
{
c1 = (4 * d[1] + 10 * d[2] + 9 * d[3] + 2 * d[4] + d[5] + 7 * d[6]) % 11;
c2 = (7 * d[1] + 8 * d[2] + 7 * d[3] + d[4] + 9 * d[5] + 6 * d[6]) % 11;
c3 = (9 * d[1] + d[2] + 7 * d[3] + 8 * d[4] + 7 * d[5] + 7 * d[6]) % 11;
c4 = (d[1] + 2 * d[2] + 9 * d[3] + 10 * d[4] + 4 * d[5] + d[6]) % 11;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
number = d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+c1+c2+c3+c4.ToString();
textBox2.Text = number;
}
它将接受第一个 TextBox
(es) 中的数字。一旦它移动到 catch 部分,它就会弹出一个错误 Index was outside the bounds of the array
有什么明显的我遗漏了吗?还是这对我的程序来说非常独特?
最佳答案
我相信您认为您的数组是从 1 到 6。 从 0 到 5。
关于c# - 索引超出数组范围 - 整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221309/