C#復習:
在控制台程序中使用結構體、集合,完成下列要求
項目要求:
一、連續輸入5個學生的信息,每個學生都有以下4個內容:
1、序號 - 根據輸入的順序自動生成,不需要手動填寫,如輸入第一個學生的序號是1,第二個是2,以此類推
2、學號 - 必填,如:S001,S002... 以此類推
3、姓名 - 必填
4、成績 - 大於等於0,小於等於100
以上內容必須按照要求填寫,請寫好相應的驗證,如果沒填寫正確,則讓用戶重復填寫到正確為止
二、5個學生信息都輸入完畢后,按照分數從高到低的順序將學生信息展示出來
顯示格式如下:
==============學生成績展示=================
序號 學號 姓名 成績
3 S003 張三 100
1 S001 李四 99
2 S002 王五 98
...
...
-------------------------------------------------------------------------------
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication4 { class Program { struct swe { public int xuhao; public string xuehao; public string xingming; public double chengji; } static void Main(string[] args) { ArrayList al = new ArrayList(); Console.WriteLine("請輸入班級人數:"); int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { swe s = new swe(); s.xuhao = i; while (true) { Console.WriteLine("請輸入第{0}個學生的學號:", i); s.xuehao = Console.ReadLine(); if (s.xuehao == "") { Console.WriteLine("學號不能為空!"); } else { break; } } while (true) { Console.WriteLine("請輸入第{0}個學生的姓名:", i); s.xingming = Console.ReadLine(); if (s.xingming == "") { Console.WriteLine("姓名不能為空!"); } else { break; } } while (true) { Console.WriteLine("請輸入第{0}個學生的成績;", i); try { s.chengji = int.Parse(Console.ReadLine()); if (s.chengji >= 0 && s.chengji <= 100) { break; } else { Console.WriteLine("成績必須在0~100之間!"); } } catch { Console.WriteLine("成績輸入有誤!"); } } al.Add(s); Console.WriteLine(); for (int e = 0; e < al.Count - 1; e++) { for (int j = e + 1; j < al.Count; j++) { swe s1 = (swe)al[e]; swe s2 = (swe)al[j]; if (s1.chengji < s2.chengji) { swe zhong = (swe)al[e]; al[e] = al[j]; al[j] = zhong; } } } } Console.WriteLine("序號 學號 姓名 成績 "); foreach (object qq in al) { Console.WriteLine(((swe)qq).xuhao + " " + ((swe)qq).xuehao + " " + ((swe)qq).xingming + " " + ((swe)qq).chengji); }