1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中 6 //最终将两个集合合并为一个集合,并且奇数显示在左边 偶数显示在右边。 7 //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 8 //List<int> listOu = new List<int>(); 9 //List<int> listJi = new List<int>(); 10 //for (int i = 0; i < nums.Length; i++) 11 //{ 12 // if (nums[i] % 2 == 0) 13 // { 14 // listOu.Add(nums[i]); 15 // } 16 // else 17 // { 18 // listJi.Add(nums[i]); 19 // } 20 //} 21 22 //listOu.AddRange(listJi); 23 //foreach (int item in listOu) 24 //{ 25 // Console.Write(item+" "); 26 //} 27 //Console.ReadKey(); 28 29 //listJi.AddRange(listOu); 30 //foreach (int item in listJi) 31 //{ 32 // Console.Write(item+" "); 33 //} 34 //Console.ReadKey(); 35 36 ////List<int> listSum = new List<int>(); 37 ////listSum.AddRange(listOu); 38 ////listSum.AddRange(listJi); 39 //Console.ReadKey(); 40 41 /* 提手用户输入一个字符串,通过foreach循环将用户输入的字符串赋值给一个字符数组 */ 42 //Console.WriteLine("请输入一个字符串"); 43 //string input = Console.ReadLine(); 44 //char[] chs = new char[input.Length]; 45 //int i = 0; 46 //foreach (var item in input) 47 //{ 48 // chs[i] = item; 49 // i++; 50 //} 51 52 //foreach (var item in chs) 53 //{ 54 // Console.WriteLine(item); 55 //} 56 //Console.ReadKey(); 57 58 /* 统计 Welcome to china中每个字符出现的次数 不考虑大小写 */ 59 string str = "Welcome to China"; 60 //字符 ------->出现的次数 61 //键---------->值 62 Dictionary<char, int> dic = new Dictionary<char, int>(); 63 for (int i = 0; i < str.Length; i++) 64 { 65 if (str[i] == ‘ ‘) 66 { 67 continue; 68 } 69 //如果dic已经包含了当前循环到的这个键 70 if (dic.ContainsKey(str[i])) 71 { 72 //值再次加1 73 dic[str[i]]++; 74 } 75 else//这个字符在集合当中是第一次出现 76 { 77 dic[str[i]] = 1; 78 } 79 } 80 81 foreach (KeyValuePair<char,int> kv in dic) 82 { 83 Console.WriteLine("字母{0}出现了{1}次",kv.Key,kv.Value); 84 } 85 Console.ReadKey(); 86 } 87 }
原文:https://www.cnblogs.com/hghd/p/13710557.html