码迷,mamicode.com
首页 > 编程语言 > 详细

用C#语言编写:集合管理器

时间:2018-03-24 14:32:29      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:AC   ati   输出   put   内容   main   list   foreach   count   

 static void Main(string[] args)
        {
            List<int> numbers = new List<int>();
            while (true)
            {
               #region 输出集合内容
                Console.WriteLine("集合中现有内容如下:");
                Console.WriteLine("===============================");
                if (numbers.Count==0)
                {
                    Console.WriteLine("集合中没有元素");
                }
                else
                {
                    foreach (int item in numbers)
                    {
                        Console.Write(item + "\t");
                    }
                    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.Write("请输入要添加的数字:");
                    int num = int.Parse(Console.ReadLine());
                    numbers.Add(num);
                    #endregion
                }
                else if (input == "2")
                {
                    #region 删除数据
                    Console.WriteLine("清输入你要删除的数据(只会删除第一个匹配项):");
                    int num = int.Parse(Console.ReadLine());
                    numbers.Remove(num);
                    #endregion
                }
                else if (input == "3")
                {
                    #region 修改数据
                    if (numbers.Count == 0)
                    {
                        Console.Write("集合中没有任何程序可以修改,按回车键继续");
                        Console.ReadLine();
                    }
                    else
                    {
                        int maxIndex = numbers.Count - 1;
                        Console.Write("请输入要删除的下标(0-" + maxIndex + ")");
                        int index = int.Parse(Console.ReadLine());
                        if (index < 0 || index > maxIndex)
                        {
                            Console.WriteLine("输入错误,下标超出范围,按回车键继续");
                            Console.ReadLine();
                        }
                        else
                        {
                            Console.Write("请输入新的数据:");
                            int newnum = int.Parse(Console.ReadLine());
                            numbers[index] = newnum;
                        }
                    }
                    #endregion
                }
                else if (input == "4")
                {
                    #region 升序排序
                    for (int i = 0; i < numbers.Count-1; i++)
                    {
                        for (int j = i+1; j < numbers.Count; j++)
                        {
                            if (numbers[i] > numbers[j])
                            {
                                int temp = numbers[i];
                                numbers[i] = numbers[j];
                                numbers[j] = temp;
                            }
                        }
                    }
                    #endregion
                }
              

               #endregion
                    //控制台清屏
                Console.Clear();
            }
        }

用C#语言编写:集合管理器

标签:AC   ati   输出   put   内容   main   list   foreach   count   

原文地址:https://www.cnblogs.com/Maigo/p/8638625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!