日期:2014-05-17  浏览次数:20667 次

麻烦大家帮忙解决一下,任意数组赋值问题
 static void Main(string[] args)
        {   //想实现任意输入数组元素个数,不用管它的长度;
            List<string >a =new  List<string>();
       
            while (true)
            {
                int i = 0;

                
                a.Add(Console.ReadLine());

                i += 1;
                if (a[i - 1] == " ")
                    break;
               
            }
            //将数组元素一一输出;
            for (int i = 0; i < a.length;i++ )
                Console.WriteLine(a[i]);
           
                Console.Read();
        }

貌似那个a.length不能这样表示,不知道为什么。而且给a中元素赋值的时候,也是很有问题,不会跳出的感觉,可以无限输入;

------解决方案--------------------

static void Main(string[] args)
{
    List<string> a = new List<string>();

    int i = 0;//如果放在循环体内,每次循环都重新声明了一个i,还是0;
    while (true)
    {
        a.Add(Console.ReadLine());

        string c = a[i];

        if (c == "")//输入文字后,连续敲两次回车就可以了;
            break;
        i++;
    }
    //将数组元素一一输出;