【发布时间】:2012-10-24 15:38:04
【问题描述】:
我刚刚遇到了一个常见的问题,但我不确定为什么会在这种情况下发生。
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 有什么明显的我遗漏了吗?还是这对我的程序来说是独一无二的?
【问题讨论】:
-
数组索引从 0 开始,你从 1 开始。这可能是原因吗?
标签: c# arrays winforms indexing