編譯一個可以給學生成績排序的student類--C#

編寫一個學生類,可以由用戶輸入學生的成績,程序來給學生排位,包含屬性get set value的簡單應用,和函數如何傳遞參數和數組,主函數怎麼使用數組,

//創建學生類Student,基本構成如下(應適當擴充):
//①私有字段name(學生姓名),公有屬性Name;
//②私有字段score(期末成績),公有屬性Score;
//要求在完善學生類構成的基礎上,將所有學生對象存放在一個Student對象數組中,
//通過方法Sort實現按照Score(期末成績)進行降序排序,並輸出排序結果

using System;

namespace 學生成績排序
{
    public class Student
    {
        private string name;
        public string Name
        {
            get
            {
                return (name);
            }
            set
            {
                name = value;
            }

        }
        private int score;
        public int Score
        {
            get
            {
                return (score);
            }
            set
            {
                if (value >= 0 && value <= 100)
                    score = value;
            }
        }
        public void Setin()
        {
              Console.WriteLine("請輸入學生的姓名:");
                name = Console.ReadLine();
                Console.WriteLine("請輸入學生的成績:");
                score = Convert.ToInt32(Console.ReadLine());           
        }
        public  void output(  )
        {           
                Console.WriteLine("學生的姓名爲:{0},成績爲:{1}", Name, Score);
        }
        public static void Sort(int n, Student[] stu)
        {
            int i, j;
            for (i = 0; i < n - 1; i++)
            {
                for (j = i + 1; j < n; j++)
                {
                    if (stu[j].Score > stu[i].Score)
                    {
                        Student a = stu[i];
                        stu[i] = stu[j];
                        stu[j] = a;
                    }
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                int n = 0;
                Console.WriteLine("請輸入學生的數量:");
                n = Convert.ToInt32(Console.ReadLine());
                Student[] stu = new Student[n];
                  for (int i = 0; i < n; i++)
                {
                    Student student = new Student();
                    student.Setin();
                    stu[i] = student;
                }
                Sort(n, stu);
                Console.WriteLine("按成績排位!");
                Console.WriteLine("學生的名次表爲:");
                for (int i = 0; i < n; i++)
                {
                    Student studenti = new Student();
                    stu[i].output();
                }
                Console.WriteLine();
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章