简体   繁体   English

索引超出范围异常数组

[英]index out of range exception array

I don't understand why I am getting an out of range error. 我不明白为什么会出现超出范围的错误。 Is my code set up improperly to have the array increase in size with each word entered? 我的代码设置是否不正确,以使输入的每个单词的数组大小都会增加? Note: I have some classes not shown here. 注意:我有些班级未在此处显示。 Please tell me what I have to do in order to make the array increase by one every time a new word is entered. 请告诉我我必须做些什么,以便每次输入一个新单词时使数组增加一。

 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...");
                }

            }



        }
    }
}

Perhaps you should use a collection like List<string> . 也许您应该使用List<string>类的集合。 A dynamic alternative to an array: 动态替代数组:

 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM