请输入学生数量:10
请输入第1个学生的姓名:sdf
请输入第1个学生的性别:男
请输入第1个学生的成绩:0~100 (不在这个范围之内,算作0分)

========学生信息展示=========
名次   姓名    性别     成绩
1      张三     男        99
2      李四     女        90

 

 

namespace 练习讲解
{
    class Program
    {
        static void Main(string[] args)
        {
            //先让用户输入学生个数
            Console.Write(“请输入学生个数:”);
            int stuNumber = Convert.ToInt32(Console.ReadLine());

            //将每个学生的信息记录
            ArrayList stus = new ArrayList();

            for (int i = 1; i <= stuNumber; i++)
            {
                Student stu = new Student();
                Console.Write(“请输入第” + i + “个学生的姓名:”);
                stu.Name = Console.ReadLine();
                Console.Write(“请输入第” + i + “个学生的性别:”);
                stu.SexStr = Console.ReadLine();
                Console.Write(“请输入第” + i + “个学生的成绩:”);
                stu.Score = Convert.ToInt32(Console.ReadLine());

                //将本次创建好的学生对象放入集合
                stus.Add(stu);
            }

            //按照成绩高低进行冒泡排序

            for (int i = 0; i < stus.Count – 1; i++)
            {
                for (int j = i + 1; j < stus.Count; j++)
                {
                    //判断每个对象的成绩大小
                    if (((Student)stus[i]).Score < ((Student)stus[j]).Score)
                    {
                        object temp = stus[i];
                        stus[i] = stus[j];
                        stus[j] = temp;
                    }
                }
            }

            //将学生信息打印显示
            Console.WriteLine(“========学生信息展示=========”);
            Console.WriteLine(“名次   姓名   性别   成绩”);
            int number = 1;
            foreach (object obj in stus)
            {
                Student s = (Student)obj;
                Console.WriteLine(number + ”   ” + s.Name + ”   ” + s.SexStr + ”   ” + s.Score);
                number++;
            }
            Console.ReadKey();
        }
    }
}

 

namespace 练习讲解
{
    class Student
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private bool _Sex;

        public bool Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }

        public string SexStr
        {
            get { return _Sex ? “男” : “女”; }
            set
            {
                if (value == “男”) _Sex = true;
                else _Sex = false;
            }
        }

        private int _Score;

        public int Score
        {
            get { return _Score; }
            set
            {
                if (value >= 0 && value <= 100) _Score = value;
                else _Score = 0;
            }
        }

    }
}

 

版权声明:本文为buzhiheyan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/buzhiheyan/p/7814380.html