using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace odds { class Program { static void Main(string[] args) { List<string> str = new List<string>(); int len = 0; int jsum =0; int osum =0; Console.WriteLine("輸出數組的元素,以q結束"); while (true) { string input = Console.ReadLine(); if (input.Equals("q") == false) //如果輸入的不是q(區分大小寫),則增加記錄 str.Insert(len++, input); else break; } //Console.WriteLine("輸出數據長度"); // Console.WriteLine(len); //結果說明數據是按行存在鏈表中的,每行占鏈表一個值 // Console.WriteLine("依次輸出鏈表中數據"); // for (int i = 0; i < len; i++) // { // Console.WriteLine(str[i]); //依次輸出鏈表每個值,也是依次輸出每行 //} //Console.WriteLine("依次輸出每個值"); string[][] every = new string[len][]; //交叉數組,行固定,為上面得到的行數,每一行的長度不定(每行字符間以空格或其他分割) for (int i = 0; i < len; i++) { every[i] = str[i].Split(); //C#對空格的分割方式之一,如果是其他分割方式,就需要也使用上面的鏈表分割每行的方式了 } //for (int i = 0; i < len; i++) //{ // for (int j = 0; j < every[i].Length; j++) // { // Console.WriteLine(every[i][j]); // } // } for (int i = 0; i < len; i++) { for (int j = 0; j < every[i].Length; j++) { int aa; // Console.WriteLine(every[i][j]); aa = int.Parse(every[i][j]); if ((aa % 2) == 1) { jsum += aa; } else { osum += aa; } } } Console.WriteLine("奇數之和為:"); Console.WriteLine(jsum); Console.WriteLine("偶數之和為:"); Console.WriteLine(osum); Console.ReadKey(); } } }