首页 > 编程语言 > 详细

定义一个学生的结构体,学号,姓名,身高,输入学生信息。按身高排序输出

时间:2015-09-20 13:09:21      阅读:2814      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        struct student 
        {
            public int code;//pu修饰符blic
            public string name;//结构体成员
            public int shengao;
        }
        static void Main(string[] args)
        {
            //结构体,用户自定义数据类型,变量组,可以一次性存多个数据变量,定义在main函数外边,class里边
            //题目:定义一个学生的结构体,学号,姓名,身高,输入学生信息。按身高排序输出
            ArrayList al = new ArrayList();//定义集合
            //录入集合
            for (int i = 1; i <= 10;i++ )
            {
                student s = new student();
                Console.Write("请输入学号");
                s.code = int.Parse(Console.ReadLine());
                Console.Write("请输入姓名");
                s.name = Console.ReadLine();
                Console.Write("请输入身高cm");
                s.shengao = int.Parse(Console.ReadLine());
                al.Add(s);
                
            }
            //冒泡排序
            for (int i = 0; i < 9; i++)
            {
                for (int j = i + 1; j < 10; j++)
                {
                    student si = (student)al[i];
                    student sj = (student)al[j];

                    if (si.shengao < sj.shengao)
                    {
                        student zhong = si;
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }

            //遍历集合
            foreach (student s in al)
            {
                Console.WriteLine(s.code + "   " + s.name + "    " + s.shengao);
            }
           
            Console.ReadLine();
        }
    }
}
技术分享

 

定义一个学生的结构体,学号,姓名,身高,输入学生信息。按身高排序输出

原文:http://www.cnblogs.com/wang-kaifeng/p/4823127.html

(1)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!