检查是否在范围内时System.IndexOutOfRangeException

时间:2014-03-12 01:43:49

标签: c# .net arrays exception

检查索引是否在范围内,但是它抛出了一个System.IndexOutOfRangeException ...理论上下面应该有效:

for (int b = 1; b <= p.Length-2; a++)

但我还是得到了

  

未处理的异常:System.IndexOutOfRangeException:索引超出了数组的范围。           在scred.Program.Main(String [] args)中的c:\ Users \\ Documents \ Visual Studio   2013 \ Projects \ CodeJam \ Store Credit - Small(C-Sharp)\ Program.cs:第29行

运行

代码:https://gist.github.com/mypalsminecraft/9498980(谁需要Pastebin?)

1 个答案:

答案 0 :(得分:3)

您好像已经完成了复制和粘贴错误:

for (int a = 1; a <= p.Length-2; a++)
                {
                    bool done = false;
                    for (int b = 1; b <= p.Length-2; a++)
                    {

应该是

for (int a = 1; a <= p.Length-2; a++)
                {
                    bool done = false;
                    for (int b = 1; b <= p.Length-2; b++)
                    {

代替。请注意b++而不是a++

相关问题