c#分割习题


2、从一个记录了学生成绩的文本文档,每个学生成绩是一行,每行是用 | 分割的数据,用 | 分割的域分别是姓名、年龄、成绩、年级,写程序取出各个年级成绩最高学生的成绩、年级放到集合中。
提示:
(1)使用 string[] lines=System.IO.File.ReadAllLines(@"c:/root.ini" ,Encoding.Default ); 从文本文件读取数据,返回值为 string 数组,每个元素是一行。
(2) root.ini 中数据可参考:
张三|18|90|2015
李四|19|100|2015
王小二|1|30|2015
张三三|18|90|2014
李四四|19|140|2014
王小小|18|30|2014



Hashtable h = new Hashtable(); string[] lines = System.IO.File.ReadAllLines(@"C:Users尘梦Desktop oot.txt", Encoding.Default); int[] scores = new int[lines.Length]; int[] years = new int[lines.Length]; for (int i = 0; i < lines.Length; i++) { string rot = lines[i]; string[] rots = rot.Split('|'); scores[i] = Convert.ToInt32(rots[2]); years[i] = Convert.ToInt32(rots[3]); } //想法知道怎么去写 但是从语法上 不会写了 //所以只能用这种看起来很笨的方法了 int max = scores[0]; int max1=scores[0]; int year = 0; int year1 = 0; int index = 0; for (int i = 0; i < years.Length; i++) { if (years[i] == 2015 || years[i] != 2014) { year = years[i]; //2015 for (int j = 0; j < 3; j++) { if (scores[i] > max) { max = scores[i]; //100 } } } else if (years[i] == 2014 || years[i] != 2015) { year1 = years[i]; //2014 for (int x = 3; x < 6; x++) { if (scores[i] > max1) { max1 = scores[i]; //140 } } } } h.Add(year1, max1);//2014 h.Add(year, max);//2015 Console.WriteLine("2015成绩{0}",h[2015]); Console.WriteLine("2014成绩{0}", h[2014]); Console.ReadKey();
Hashtable ht = new Hashtable();
            string[] lines = System.IO.File.ReadAllLines(@"C:Documents and SettingsAdministrator桌面
oot.ini", Encoding.Default);
            foreach (string l in lines) {
                string[] root = l.Split('|');
                if (ht.ContainsKey(root[3]))
                {
                    string cj = ht[root[3]].ToString();
                    if (int.Parse(cj) < int.Parse(root[2])) {

                        ht[root[3]] =root[2] ;
                    
                    }

                }
                else {

                    ht.Add(root[3], root[2]);
                
                }
            
            
            }
            foreach (var item in ht.Keys) {

                Console.WriteLine("年级为{0},最高成绩为{1}",item,ht[item]);
            }
            Console.ReadKey();
原文地址:https://www.cnblogs.com/mengluo/p/4904624.html