在案例切换数组中发生'System.IndexOutOfRangeException'

时间:2015-04-06 03:28:45

标签: arrays switch-statement case indexoutofrangeexception

在我第一次做这个的时候,在一年或两年前的地方投掷'System.IndexOutOfRangeException'。我问我现在的教授,但他只是说“它的工作没有问题”我们都得到了例外,所以我老师不这样做。这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Author: Austin Bigge
* This program will ask for a ten digit alphanumeric
* number and return the numeric number.
*/
namespace alphanumeric
{
class Program
{
    static void Main(string[] args)
    {
        // Switch for alphanumeric entry
        string numberString; //alphanumberic entry
        char currentLetter;
        string invalue;
        double pnumber; //returned single numeric phone number
        int i; //i = the current character space

        //User prompt
        Console.WriteLine("This program will ask for an alphanumberic phone ");
        Console.WriteLine("number and change it into a numeric phone number.");
        Console.WriteLine("! Alphanumeric characters only !");
        Console.WriteLine("! No Spaces, Dashes, or Symbols !");
        Console.Write("What is the 10 digit phone number? ");
        invalue = Console.ReadLine();
        numberString = invalue;

        char[] array = numberString.ToCharArray();
        for (i = 0; i < array.Length; i++) //for loop
        // gets the current letter at position "i"
        currentLetter = array[i];
        switch (i) //case switch for possible entries
        {
            case '1':
                pnumber = 1; Console.WriteLine(pnumber); break;
            case '2':
            case 'a':
            case 'A':
            case 'b':
            case 'B':
            case 'c':
            case 'C':
                pnumber = 2; Console.WriteLine(pnumber); break;
            case '3':
            case 'd':
            case 'D':
            case 'e':
            case 'E':
            case 'f':
            case 'F':
                pnumber = 3; Console.WriteLine(pnumber); break;
            case '4':
            case 'g':
            case 'G':
            case 'h':
            case 'H':
            case 'i':
            case 'I':
                pnumber = 4; Console.WriteLine(pnumber); break;
            case '5':
            case 'j':
            case 'J':
            case 'k':
            case 'K':
            case 'l':
            case 'L':
                pnumber = 5; Console.WriteLine(pnumber); break;
            case '6':
            case 'm':
            case 'M':
            case 'n':
            case 'N':
            case 'o':
            case 'O':
                pnumber = 6; Console.WriteLine(pnumber); break;
            case '7':
            case 'p':
            case 'P':
            case 'q':
            case 'Q':
            case 'r':
            case 'R':
            case 's':
            case 'S':
                pnumber = 7; Console.WriteLine(pnumber); break;
            case '8':
            case 't':
            case 'T':
            case 'u':
            case 'U':
            case 'v':
            case 'V':
                pnumber = 8; Console.WriteLine(pnumber); break;
            case '9':
            case 'w':
            case 'W':
            case 'x':
            case 'X':
            case 'y':
            case 'Y':
            case 'z':
            case 'Z':
                pnumber = 9; Console.WriteLine(pnumber); break;
            case '0':
                pnumber = 0; Console.WriteLine(pnumber); break;
            case ' ': Console.WriteLine("Bad Value, do not use spaces"); break;
            default: Console.WriteLine("Bad Value, use only numbers or letters."); break;


        }

    }
}

}

任何帮助都表示赞赏,他一直在向我寻求帮助,我一直告诉他,他和我一样了解。 附:调试器指向第36行的此异常

编辑:

是的,你是对的,现在我在第37行“currentLetter”有一个未分配的变量,我已经初始化了它,我想,当我试图初始化它的错误时它告诉我它已经初始化了......我必须删除了我的工作计划。我发现了这个文件的几个空迭代。我非常感谢你的帮助,我的朋友也一定会这样。

我想我修好了,我用“i”替换了“currentLetter”

现在数组无效。只返回默认情况。卡住了。我编辑了我的进展。

修改

当我们把头放在一起时,发现了一堆错误。完成的计划如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
    static void Main(string[] args)
    {
        // Switch for alphanumeric entry
        string numberString; //alphanumberic entry
        char currentLetter;
        string invalue;
        double pnumber; //returned single numeric phone number
        int i; //i = the current character space

        //User prompt
        Console.WriteLine("This program will ask for an alphanumberic phone ");
        Console.WriteLine("number and change it into a numeric phone number.");
        Console.WriteLine("! Alphanumeric characters only !");
        Console.WriteLine("! No Spaces, Dashes, or Symbols !");
        Console.Write("What is the 10 digit phone number? ");
        invalue = Console.ReadLine();
        numberString = invalue;

        char[] array = numberString.ToCharArray();
        for (i = 0; i < 10; i++)
        {
            //for loop
            //numberString = invalue.ToString().ToCharArray()[i]);
            // gets the current letter at position "i"
            currentLetter = array[i];
            switch (currentLetter) //case switch for possible entries
            {
                case '0':
                    pnumber = 0; Console.Write(pnumber); break;

                case '1':
                    pnumber = 1; Console.Write(pnumber); break;
                case '2':
                case 'a':
                case 'A':
                case 'b':
                case 'B':
                case 'c':
                case 'C':
                    pnumber = 2; Console.Write(pnumber); break;
                case '3':
                case 'd':
                case 'D':
                case 'e':
                case 'E':
                case 'f':
                case 'F':
                    pnumber = 3; Console.Write(pnumber); break;
                case '4':
                case 'g':
                case 'G':
                case 'h':
                case 'H':
                case 'i':
                case 'I':
                    pnumber = 4; Console.Write(pnumber); break;
                case '5':
                case 'j':
                case 'J':
                case 'k':
                case 'K':
                case 'l':
                case 'L':
                    pnumber = 5; Console.Write(pnumber); break;

                case '6':
                case 'm':
                case 'M':
                case 'n':
                case 'N':
                case 'o':
                case 'O':
                    pnumber = 6; Console.Write(pnumber); break;

                case '7':
                case 'p':
                case 'P':
                case 'q':
                case 'Q':
                case 'r':
                case 'R':
                case 's':
                case 'S':
                    pnumber = 7; Console.Write(pnumber); break;

                case '8':
                case 't':
                case 'T':
                case 'u':
                case 'U':
                case 'v':
                case 'V':
                    pnumber = 8; Console.Write(pnumber); break;

                case '9':
                case 'w':
                case 'W':
                case 'x':
                case 'X':
                case 'y':
                case 'Y':
                case 'z':
                case 'Z':
                    pnumber = 9; Console.Write(pnumber); break;

                case ' ':
                    Console.WriteLine();
                    Console.WriteLine("Bad Value, do not use spaces");
                    Console.Write("What is the 10 digit phone number? ");
                    invalue = Console.ReadLine();
                    numberString = invalue;
                    array = numberString.ToCharArray();
                    i = 0;

                    break;
                default:
                    Console.WriteLine();
                    Console.WriteLine("Bad Value, use only numbers or letters.");
                    Console.Write("What is the 10 digit phone number? ");
                    invalue = Console.ReadLine();
                    numberString = invalue;
                    array = numberString.ToCharArray();
                    i = 0;
                    break;

            }

        }
        Console.WriteLine();
        Console.WriteLine("Press any key to close this window");
        Console.ReadLine();
    }
}
}

感谢您让我们开始修复,我们都很感激,朋友!

1 个答案:

答案 0 :(得分:2)

for行上的分号是造成问题的原因:

for (i = 0; i < array.Length; i++) ; //for loop

for循环之后应该是一个块,但在这种情况下,由于它后跟一个分号,所以后面的代码不是循环的一部分,而只是常规代码& #39; s执行一次。

所以for循环用空体执行,当它完成时,i有一个数组长度的值,当然高于最大值index,抛出异常。

要解决此问题,请在循环中包含的代码组周围创建一个块(大括号)。

这也会带来一个单独的问题 - for循环通常会在循环中声明索引器 - 如果你在i中定义了for,那么它本来就是超出以下行的范围,并且更容易看到您的错误,因为这将是编译器错误。

for (int i = 0; i < array.Length; i++)
{
    // Your code here
}
// i is no longer in scope