码迷,mamicode.com
首页 > 其他好文 > 详细

集合管理器的代码实现

时间:2018-04-07 22:55:23      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:代码   NPU   ++   post   parse   i++   一个   clear   while   

  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 + "\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.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();

            }

集合管理器的代码实现

标签:代码   NPU   ++   post   parse   i++   一个   clear   while   

原文地址:https://www.cnblogs.com/cj-18/p/8734896.html

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