c#复习-2
輸入三個學(xué)生的信息
學(xué)號、姓名、分?jǐn)?shù)
從大到小排序
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 7 namespace 復(fù)習(xí)CS 8 { 9 class Program 10 { 11 struct Student 12 { 13 public int num; 14 public string Code; 15 public string Name; 16 public decimal Score; 17 } 18 19 static void Main(string[] args) 20 { 21 //1、循環(huán)添加學(xué)生信息 22 ArrayList list = new ArrayList(); 23 24 for (int i = 1; i < 4; i++) 25 { 26 Student s = new Student(); //實(shí)例化 27 28 Console.Write("請輸入第" + i + "個學(xué)生的學(xué)號:"); 29 s.Code = Console.ReadLine(); 30 Console.Write("請輸入第" + i + "個學(xué)生的姓名:"); 31 s.Name = Console.ReadLine(); 32 Console.Write("請輸入第" + i + "個學(xué)生的成績:"); 33 s.Score = Convert.ToDecimal(Console.ReadLine()); 34 s.num = i; 35 36 list.Add(s); 37 Console.WriteLine("==============================="); 38 } 39 40 Console.WriteLine("-----------------------學(xué)生數(shù)據(jù)展示--------------------------"); 41 42 //2、排序 43 44 for (int i = 0; i < list.Count - 1; i++) 45 { 46 for (int j = i + 1; j < list.Count; j++) 47 { 48 Student s1 = (Student)list[i]; 49 Student s2 = (Student)list[j]; 50 51 if (s1.Score < s2.Score) 52 { 53 Object ob = list[i]; 54 list[i] = list[j]; 55 list[j] = ob; 56 } 57 } 58 } 59 60 //3、打印 61 foreach (object o in list) 62 { 63 Student ss = (Student)o; 64 Console.WriteLine("第" + ss.num + "個學(xué)生的學(xué)號:" + ss.Code + ",姓名:" + ss.Name + ",分?jǐn)?shù):" + ss.Score + "。"); 65 } 66 67 68 69 Console.ReadKey(); 70 } 71 } 72 }
學(xué)號、姓名、分?jǐn)?shù)
從大到小排序
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 7 namespace 復(fù)習(xí)CS 8 { 9 class Program 10 { 11 struct Student 12 { 13 public int num; 14 public string Code; 15 public string Name; 16 public decimal Score; 17 } 18 19 static void Main(string[] args) 20 { 21 //1、循環(huán)添加學(xué)生信息 22 ArrayList list = new ArrayList(); 23 24 for (int i = 1; i < 4; i++) 25 { 26 Student s = new Student(); //實(shí)例化 27 28 Console.Write("請輸入第" + i + "個學(xué)生的學(xué)號:"); 29 s.Code = Console.ReadLine(); 30 Console.Write("請輸入第" + i + "個學(xué)生的姓名:"); 31 s.Name = Console.ReadLine(); 32 Console.Write("請輸入第" + i + "個學(xué)生的成績:"); 33 s.Score = Convert.ToDecimal(Console.ReadLine()); 34 s.num = i; 35 36 list.Add(s); 37 Console.WriteLine("==============================="); 38 } 39 40 Console.WriteLine("-----------------------學(xué)生數(shù)據(jù)展示--------------------------"); 41 42 //2、排序 43 44 for (int i = 0; i < list.Count - 1; i++) 45 { 46 for (int j = i + 1; j < list.Count; j++) 47 { 48 Student s1 = (Student)list[i]; 49 Student s2 = (Student)list[j]; 50 51 if (s1.Score < s2.Score) 52 { 53 Object ob = list[i]; 54 list[i] = list[j]; 55 list[j] = ob; 56 } 57 } 58 } 59 60 //3、打印 61 foreach (object o in list) 62 { 63 Student ss = (Student)o; 64 Console.WriteLine("第" + ss.num + "個學(xué)生的學(xué)號:" + ss.Code + ",姓名:" + ss.Name + ",分?jǐn)?shù):" + ss.Score + "。"); 65 } 66 67 68 69 Console.ReadKey(); 70 } 71 } 72 }
轉(zhuǎn)載于:https://www.cnblogs.com/tonyhere/p/5592236.html
總結(jié)
- 上一篇: jQuery / zepto ajax
- 下一篇: 钉钉小程序 DEMO的坑