码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-06-17 07:24:24      阅读:862      评论:0      收藏:0      [点我收藏+]

标签:

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();

        }
    }
}

技术分享

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

标签:

原文地址:http://www.cnblogs.com/fengsantianya/p/5592686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!