zoukankan      html  css  js  c++  java
  • C#例题:输入学生学号,姓名,分数,然后根据分数进行排序。这个题是用集合和结构体来做,与上一题不同。掌握基础知识很重要

     class Program
        {
            struct student
            {
                public string name;
                public int code;
                public int age;
                public double fenshu;
                                                                                                                                                                                                                                                                                    
            }
    
            static void Main(string[] args)
            {
                ArrayList al = new ArrayList();  //定义一个新的集合
                Console.Write("请输入人数:");
                int renshu = Convert.ToInt32(Console .ReadLine ());
                for (int i = 0; i <renshu; i++)
                {
                    student r = new student();
                    Console.Write("请输入第"+(i+1)+"个人的名字:");
                    r.name = Console.ReadLine();
    
                    Console.Write("请输入第" + (i + 1) + "个人的学号:");
                    r.code = Convert.ToInt32(Console .ReadLine ());
                    Console.Write("请输入第" + (i + 1) + "个人的年龄:");
                    r.age = Convert.ToInt32(Console .ReadLine ());
                    Console.Write("请输入第" + (i + 1) + "个人的分数:");
                    r.fenshu = Convert.ToDouble(Console .ReadLine ());
                    al.Add(r);                                            //把r的数据都放在al这个集合里
                }
                    ; for (int i = 0; i < renshu ; i++)
                    {
                        for (int j = i; j < renshu-1; j++)
                        
                            if (((student)al[i]).fenshu<((student )al[j+1]).fenshu)  //从student 结构体的集合里调出分数进行比较
                            {
                                student zhong;
                                zhong = (student )al[i];
                                al[i] = al[j + 1];
                                al[j+ 1] = zhong;
                            }
                        }
                Console .WriteLine ("排序后学生顺序:");
                    for (int i = 0; i < renshu; i++)
                    {
                        Console.WriteLine("名字	" + ((student)al[i]).name + "学号	" + ((student)al[i]).code + "年龄	" + ((student)al[i]).age
                            + "分数	" + ((student)al[i]).fenshu );
                       
                    }
                    Console.ReadLine();
                    }
                }
            }
  • 相关阅读:
    2018年5月份
    2018年4月份
    2018年3月份
    2018年2月份
    代码书写欣赏
    关于Model层中Datetime Datetime? 默认值的问题
    关于DotNetBar中DataGridViewX 自动全屏 Anchor属性无效问题
    关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )
    ios 关于StoryBoard 的简易使用说明
    VS2012 Build相关
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4463158.html
Copyright © 2011-2022 走看看