【问题标题】:index out of range exception array索引超出范围异常数组
【发布时间】:2014-03-29 03:32:38
【问题描述】:

我不明白为什么会出现超出范围的错误。我的代码是否设置不正确,使数组的大小随着输入的每个单词而增加?注意:我有一些课程没有在这里显示。请告诉我每次输入一个新单词时我必须做什么才能使数组增加一。

 class Program
{
    static String[] Parse(String commandLine)
    {
        string[] stringSeparators = new string[] { "" };
        String[] localList = new String[commandLine.Count((char f) => f == ' ') + 1];
        localList = commandLine.Split(stringSeparators, StringSplitOptions.None);
        return localList;
    }
    static void Main(string[] args)
    {
        Verb cVerb = new Verb(); //constructors begin here
        Noun cNoun = new Noun();
        Item itemName = new Item();
        Player myPlayer = new Player();
        String commandLine;

        Room northRoom = new Room();
        Room southRoom = new Room();

        northRoom.setRoomDesccription("You stand in the center of the northern room. It smells musty");
        southRoom.setRoomDesccription("You stand in the center of the southern room. It is made of crumbling stone");

        myPlayer.setRoom(northRoom);

        Console.WriteLine("Welcome young hero, to the world of Argandia");           
        while (cVerb.getName() != "Exit") // continue playing as long as verb entered =/ "exit"
        {
            //Room Description
            myPlayer.getRoom().printDesc();

            //Player Input
            Console.WriteLine("You can now type a verb then a noun");
            commandLine = Console.ReadLine();
            int numWords = commandLine.Count((char f) => f == ' ') + 1;
            String[] verbNounList = new String[numWords];
            verbNounList = Parse(commandLine);



            //process input
            if (numWords > 0)
            {
                cVerb.setName(verbNounList[0]);
                if (cVerb.getName() == "Exit")
                {
                    Console.WriteLine("Thanks for playing\nSee you next time\nPress any key to exit...");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
                if (numWords > 1)
                {
                    cNoun.setName(verbNounList[1]);
                }
                if (numWords == 2)
                {

                }
                else
                {
                    Console.WriteLine("We are only able to support 2 words at this time, sorry\nPress any key to continue...");
                }

            }



        }
    }
}

【问题讨论】:

    标签: c# windows indexoutofrangeexception


    【解决方案1】:

    也许您应该使用像List<string> 这样的集合。数组的动态替代:

     public static IEnumerable<string> Parse(string commandLine)
        {
            foreach (var word in commandLine.Split(' '))
                yield return word;
        }
    
        static void Main(string[] args)
        {
            string testCommandLine = "Hello World";
            var passedArgs = Parse(testCommandLine);
            foreach (var word in passedArgs)
            { 
                //do some work
                Console.WriteLine(word);
            }
    
            Console.Read();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-05
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 2012-02-25
      相关资源
      最近更新 更多