zoukankan      html  css  js  c++  java
  • 定义一个学生的结构体,学号,姓名,身高,输入学生信息。按身高排序输出

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

  • 相关阅读:
    动态创建多个pictureBox控件并响应不同的事件
    浅谈程序效率问题
    winform 下实现消息传递机制
    兼容IE的最小最大高度CSS写法
    推荐14款web开源测试工具
    winform程序窗体相关设置
    Mysql配置及错误集合
    深入理解栈
    广而博,深而远
    POJ3009
  • 原文地址:https://www.cnblogs.com/wang-kaifeng/p/4823127.html
Copyright © 2011-2022 走看看