附加到C#中的字符串数组

时间:2017-12-11 06:20:19

标签: c#

    string Input = "";
    string[] Words = { "elephant", "lion" };
    string[] Clues = { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?"};

.........

        Console.WriteLine("Do you want to add you own animal? y/n ? \n");
        Input = Console.ReadLine();
        if (Input == "Y" || Input == "y")
        {
            Console.WriteLine("Enter an animal name: \n");
            //Array.Resize(ref Words, Words.Length + 1);
            Input = Console.ReadLine();
            Words[Words.Length] = Input;


            Console.WriteLine("Enter 2 clues \n");
            for (int i = 1; i <=2 ; i++)
            {
                Console.WriteLine("Clue" + i + ":");
                Clues[Clues.Length] = Console.ReadLine();

            }

        }

这是动物游戏的标准猜测.. 我在第index out of bounds行获得 Words[Words.Length] = Input; ..下次我玩游戏时,新的动物和线索也需要提供..

5 个答案:

答案 0 :(得分:2)

而是string []使用来自 System.Collections.Generic List<T>

您可以使用Add方法添加新值。

Console.WriteLine("Enter an animal name: \n");
Input = Console.ReadLine();
Words.Add(Input);

如果最后想要一个数组,可以使用ToArray方法。像这样。

Words.ToArray();

答案 1 :(得分:0)

将字符串添加到有限数组(例如您声明的数组)会导致此附加项超出此数组的范围,即。你正在把更多的东西挤进一个无法容纳这个数量的空间。一旦创建(在编译时),这个大小是固定的,直到下次才能改变。

我认为你要找的是List<string>列表,它们使用list.Add()在运行时动态地操作内容。

如果您回答了问题,或者您需要更多详细信息,请告诉我。

答案 2 :(得分:0)

您可以使用List<string>代替Array。您的代码抛出异常,因为您尝试将元素添加到outranged索引中的Array。我像这样修改了你的代码;

        string Input = "";
        var Words = new List<string> { "elephant", "lion" };
        var Clues = new List<string> { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?" };
        Console.WriteLine("Do you want to add you own animal? y/n ? \n");
        Input = Console.ReadLine();
        if (Input == "Y" || Input == "y")
        {
            Console.WriteLine("Enter an animal name: \n");
            //Array.Resize(ref Words, Words.Length + 1);
            Input = Console.ReadLine();
            Words.Add(Input);


            Console.WriteLine("Enter 2 clues \n");
            for (int i = 1; i <= 2; i++)
            {
                Console.WriteLine("Clue" + i + ":");
                var clueInput = Console.ReadLine();
                Clues.Add(clueInput);

            }

        }

答案 3 :(得分:0)

为什么不使用List而不是Array。

     string Input = "";
    List<string> Words = new List<string>(){ "elephant", "lion" };
    List<string>  Clues =new List<string>() { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?"};

 Console.WriteLine("Do you want to add you own animal? y/n ? \n");
    Input = Console.ReadLine();
    if (Input.toLower() == "y")
    {
        Console.WriteLine("Enter an animal name: \n");
        //Array.Resize(ref Words, Words.Length + 1);
        Input = Console.ReadLine();
        Words.Add(Input);


        Console.WriteLine("Enter 2 clues \n");
        for (int i = 1; i <=2 ; i++)
        {
            Console.WriteLine("Clue" + i + ":");
            Clues.Add(Console.ReadLine());

        }

    }

请遵循此。

答案 4 :(得分:0)

`Console.WriteLine("Enter an animal name: \n");
//Hear Words length is 2 and Data (0 = elephant,1 = lion)
Array.Resize(ref Words, Words.Length + 1);
//Hear Words length is 3 and Data (0 = elephant,1 = lion,2 = null)
Input = Console.ReadLine();
//if you write Words[Words.Length] means you tried to access 3 index which is not available.
//You should write this.
Words[Words.Length - 1] = Input;`