C#雜湊表實現新增學生查詢全部學生資訊
阿新 • • 發佈:2018-12-15
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentsSystemHx { class Program { static void Main(string[] args) { Console.WriteLine("歡迎使用2018級學生管理系統"); Console.WriteLine("==============================================="); Console.WriteLine("請選擇如下操作:"); Console.WriteLine(); Console.WriteLine("A:新增 B:查詢 C:刪除 D:修改 E退出系統"); Hashtable stu = new Hashtable(); Students student = new Students(); string ch; Console.Write("請輸入選擇的功能:"); ch = Console.ReadLine(); while(ch!="E") { switch(ch) { case "A": AddStudent(stu); break; case "B": QueryStudent(stu); break; case "C": break; case "D": break; case "E": break; } Console.Write("請輸入選擇的功能:"); ch = Console.ReadLine(); } } public static void AddStudent(Hashtable stu) { string sno; Console.WriteLine("請輸入新增學生的資訊"); Console.WriteLine("請輸入學生學號:"); sno = Console.ReadLine(); while(stu.Contains(sno)) { Console.WriteLine("該學生已存在,請重新輸入:"); sno = Console.ReadLine(); } Students s= new Students(); Console.WriteLine("請輸入學生姓名:"); string name = Console.ReadLine(); Console.WriteLine("請輸入學生性別:"); string sex = Console.ReadLine(); Console.WriteLine("請輸入學生班級:"); string class_ = Console.ReadLine(); Console.WriteLine("請輸入學生語文成績:"); double chinese = double.Parse(Console.ReadLine()); Console.WriteLine("請輸入學生數學成績:"); double math = double.Parse(Console.ReadLine()); Console.WriteLine("請輸入學生英語成績:"); double english = double.Parse(Console.ReadLine()); s.setSno(sno); s.setName(name); s.setSex(sex); s.setClass_(class_); s.setChinese(chinese); s.setMath(math); s.setEnglish(english); stu.Add(sno,s); } public static void QueryStudent(Hashtable stu) { string sno; Console.WriteLine("請輸入查詢學生的學號;"); sno = Console.ReadLine(); ICollection c = stu.Values; IEnumerator ie=c.GetEnumerator(); while (ie.MoveNext()) { string s = ((Students)ie.Current).showMsg(); Console.WriteLine(s); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentsSystemHx { class Students { string sno; string name; string sex; string class_; double chinese; double math; double english; //public Students( string sno,string name, string sex,string class_,double chinese,double math, double english) //{ //} public void setSno(string sno) { this.sno = sno; } public void setName(string name) { this.name = name; } public void setSex(string sex) { this.sex = sex; } public void setClass_(string class_) { this.class_= class_; } public void setChinese(double chinese) { this.chinese = chinese; } public void setMath(double math) { this.math = math; } public void setEnglish(double english) { this.english = english; } public string showMsg() { Console.WriteLine("學號:{0}", sno); Console.WriteLine("姓名:{0}", name); Console.WriteLine("性別:{0}", sex); Console.WriteLine("班級:{0}", class_); Console.WriteLine(" 語文成績 " + chinese + " 數學成績 " + math + " 英語成績 " + english); return sno + " " + " 姓名 " + name; } } }