初学者使用一维数组定义,怎么让用户自己输入长度和数据

img

定义一个足够大的数组,或者根据输入的数组大小用malloc功态分配空间


                Console.Write("请输入数组长度:");
                int[] arr = new int[Convert.ToInt32(Console.ReadLine())];
                string[] n = new string[0];
                while (n.Length != arr.Length)
                {
                    Console.Write(String.Format("请输入{0}个整数,以空格分隔:", arr.Length));
                    n = Console.ReadLine().Split(' ');
                    bool success = true;
                    for (int i = 0; i < n.Length; i++)
                    {
                        int x = 0;
                        if (int.TryParse(n[i], out x))
                        {
                            arr[i] = x;
                        }
                        else
                        {
                            success = false;
                            break;
                        }
                    }
                    if (success)
                    {
                        break;
                    }
                }
                Console.Write(String.Format("你输入的数字是{0},最大值是{1},最小值是{2}。", String.Join(",", n), arr.Max(), arr.Min()));

                Console.ReadKey();

img