List<int> a = new List<int>(); while (true) { #region//输出集合内容 Console.WriteLine("集合中现有的内容如下:"); Console.WriteLine("==================================================================="); if (a.Count == 0) { Console.WriteLine("集合中的没有元素"); } else { foreach (int item in a) { Console.Write(item + " "); } Console.WriteLine(); } Console.WriteLine("==================================================================="); #endregion #region//提示菜单,获取用户输入的菜单选项 Console.WriteLine("1.添加数据"); Console.WriteLine("2.删除数据"); Console.WriteLine("3.修改数据"); Console.WriteLine("4.升序排序"); Console.WriteLine("0.退出程序"); Console.Write("请选择(0-4):"); string input = Console.ReadLine(); #endregion #region//根据用户输入的不同,做不同的处理 if (input == "0") { break; } else if (input == "1") { #region//添加数据 Console.WriteLine("请输入要添加的数据:"); int num = int.Parse(Console.ReadLine()); a.Add(num); #endregion } else if (input == "2") { #region//删除数据 Console.WriteLine("请输入你要删除的数据(只会删除第一个匹配项):"); int num = int.Parse(Console.ReadLine()); a.Remove(num); #endregion } else if (input == "3") { #region//修改数据 if (a.Count == 0) { Console.WriteLine("集合中没有任何数据可以修改,按回车键继续"); Console.ReadLine(); } else { int maxIndex = a.Count - 1; Console.WriteLine("请输入要删除的下标(0-" + maxIndex + ")"); int index = int.Parse(Console.ReadLine()); if (index < 0 || index > maxIndex) { Console.Write("输入错误,下标超出范围,按回车键继续"); Console.ReadLine(); } else { Console.Write("请输入新的数据:"); int newNum = int.Parse(Console.ReadLine()); a[index] = newNum; } } #endregion } else if (input == "4") { #region//升序排序 for (int i = 0; i < a.Count - 1; i++) { for (int j = i +1; j < a.Count; j++) { if (a[i] > a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } #endregion } #endregion //控制台清屏 Console.Clear(); }