- 最後登錄
- 2023-4-9
- 在線時間
- 0 小時
- 註冊時間
- 2007-7-16
- 閱讀權限
- 20
- 精華
- 0
- UID
- 1820716
- 帖子
- 177
- 積分
- 6 點
- 潛水值
- 7641 米
| 回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。 - class Program
- {
- static void Main(string[] args)
- {
- List<Student> list = new List<Student>();
- Console.Write("請輸入要建立的學生人數:");
- //不考慮輸入非數字
- int createStudents = int.Parse(Console.ReadLine());
- Console.WriteLine("*******************************************");
- Console.WriteLine("開始建立學生成績資料:");
- for(int i = 0; i < createStudents; i++)
- {
- Console.WriteLine($"第{i+1}位學生:");
- Console.Write("學號:");
- string stdId = Console.ReadLine();
- Console.Write("姓名:");
- string stdName = Console.ReadLine().Trim();
- Console.Write("國文分數:");
- int stdChi = Convert.ToInt32( Console.ReadLine());
- Console.Write("英文分數:");
- int stdEng = Convert.ToInt32(Console.ReadLine());
- Console.Write("計概分數:");
- int stdBcc = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("*******************************************");
- list.Add(new Student(stdId, stdName, stdChi, stdEng, stdBcc));
- }
- Console.Write("請輸入要查詢的學生姓名:");
- string name = Console.ReadLine();
- Student findStudent = list.Where(s => s.Name == name.Trim()).FirstOrDefault();
- if(findStudent != null)
- {
- Console.WriteLine($"學號:{findStudent.Id},姓名:{findStudent.Name}");
- Console.WriteLine($"國文成績:{findStudent.Chi}");
- Console.WriteLine($"英文成績:{findStudent.Eng}");
- Console.WriteLine($"計概成績:{findStudent.Bcc}");
- Console.WriteLine(String.Format("平均成績:{0:C2}",findStudent.GetAvg()));
- Console.WriteLine("*******************************************");
- }
- else
- {
- Console.WriteLine("找不到該位學生");
- }
- Console.ReadKey();
- }
- }
- public class Student
- {
- static int Count { get; set; } //現有學生數
- public string Id { get; set; }
- public string Name { get; set; }
- public int Chi { get; set; }
- public int Eng { get; set; }
- public int Bcc { get; set; }
- public Student()
- {
- Count++;
- }
- public Student(string id, string name, int chi, int eng, int bcc)
- :this()
- {
- this.Id = id;
- this.Name = name;
- this.Chi = chi;
- this.Eng = eng;
- this.Bcc = bcc;
- }
- public double GetAvg()
- {
- return (Chi + Eng + Bcc) / 3;
- }
- public static int GetStudentNumber() //學生數量
- {
- return Count;
- }
- }
複製代碼 ... |
|