C#使用结构体,输入5个人的学号,姓名,分数,按照成绩高低排列打印出来

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {
        struct student
        {
            public string sno;
            public string name;
            public double score;
        }
        static void Main(string[] args)
        {
            //1、循环添加学生信息
            ArrayList al = new ArrayList();
            for (int i = 1; i <= 3;i++ )
            {
                student st = new student();
                Console.Write("请输入学生编号:");
                st.sno = Console.ReadLine();
                Console.Write("请输入学生姓名:");
                st.name = Console.ReadLine();
                Console.Write("请输入学生分数:");
                st.score = double.Parse(Console.ReadLine());
                al.Add(st);
                Console.WriteLine("-------------------------");
            }

            //打印
            Console.WriteLine("按照成绩表打印");
            foreach(object o in al)
            {
                student x = (student)o;
                Console.WriteLine(x.sno+"\t"+x.name+"\t"+x.score);
            }
            //2、排序
            for (int i = 0; i < al.Count;i++ )
            {
                for (int j = i + 1; j < al.Count;j++ )
                {
                    student a = (student)al[i];
                    student b=(student)al[j];
                    if(a.score<b.score)
                    {
                        object zhong=al[i];
                        al[i]=al[j];
                        al[j] = zhong;
                    }
                }
            }
            //3、打印
            Console.WriteLine("==================================================");
            Console.WriteLine("按照成绩从大到小排列");
            foreach(object ob in al)
            {
                student o = (student)ob;
                Console.WriteLine(o.sno+"\t"+o.name+"\t"+o.score);

            }
            

            Console.ReadLine();

        }
    }
}

转载于:https://www.cnblogs.com/fengsantianya/p/5592686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值