1. 程式人生 > >集合練習_3

集合練習_3

str read .com logs lis 分享 ring con adk

技術分享

        static void Main(string[] args)
        {
            //案例:把分揀奇偶數的程序用泛型實現。List<int>
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            List<int> left = new List<int>();   //偶數放左邊
            List<int> right = new List<int>();  //奇數放右邊

            for
(int i = 0; i < nums.Length; i++) { if (nums[i] % 2 == 0) { left.Add(nums[i]); } else { right.Add(nums[i]); } } //輸出 Console.Write("
偶數是:"); foreach (var item in left) { Console.Write(item + ""); } Console.Write("/"); Console.Write("奇數是:"); foreach (var item in right) { Console.Write(item + ""); } Console.ReadKey(); }

集合練習_3