AI智能
改变未来

C#学习.集合练习2

        static void Main(string[] args)
        {
            //将一个数组中奇数放到一个集合中,再将偶数放到另外一个集合中
            //最终将两个集合合并为一个集合,奇数在左边显示,偶数在右边显示
            int[] nums = new int[] { 1, 4, 3, 2, 6, 7, 8, 11, 13, 9, 10, 12, 15, 14 };
            List<int> list1 = new List<int>();
            List<int> list2 = new List<int>();
            foreach(var num in nums)
            {
                if(num%2==1)
                {
                    list1.Add(num);
                }
                else
                {
                    list2.Add(num);
                }
            }
            list1.AddRange(list2);
            foreach(var item in list1)
            {
                Console.Write(item + \” \”);
            }

 

            //让用户输入一个字符串,通过foreach将字符串赋值给一个字符数组
            Console.WriteLine(\”请输入一个字符串:\”);
            string input = Console.ReadLine();
            char[] chs = new char[input.Length];
            int i = 0;
            foreach(var item in input)
            {
                chs[i] = item;
                i++;
            }

            //统计welcome to china中每个字符出现的次数,不考虑大小写
            string str = \”welcome to china\”;
            Dictionary<char, int> dic = new Dictionary<char, int>();
            foreach(var item in str)
            {
                if(dic.ContainsKey(item))
                {
                    dic[item]++;
                }
                else
                {
                    dic.Add(item, 1);
                }
            }

            foreach(KeyValuePair<char,int> kc in dic )
            {
                Console.WriteLine(\”{0}     {1}\”, kc.Key, kc.Value);
            }

            Console.ReadKey();
        }

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#学习.集合练习2