• 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+"	"+x.name+"	"+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+"	"+o.name+"	"+o.score);
    
                }
                
    
                Console.ReadLine();
    
            }
        }
    }

    上午,忙了半天统计排名,在网上发现这条方法的好处,于是转发引用一下

    转自:http://www.cnblogs.com/fengsantianya/p/5592686.html

  • 相关阅读:
    面向过程
    生成器
    迭代器
    装饰器
    函数及嵌套
    字符编码与文件操作
    linux_ssh
    LNMP
    BZOJ 3238: [Ahoi2013]差异
    BZOJ 3998: [TJOI2015]弦论
  • 原文地址:https://www.cnblogs.com/licongzhuo/p/8242462.html
Copyright © 2020-2023  润新知