zoukankan      html  css  js  c++  java
  • 结构体

    结构体:
    在内存中临时存储数据的方式
    1、变量、常量
    2、数组,可以存储固定长度统一类型的数据
    3、集合
    4、结构体
    
    学生信息:姓名,性别,年龄,系别
    
    结构体含义:就是将生活中的一些个体,封装成一个特殊的类型
    
    结构体是:用户自定义数据类型
    
    创建:
    创建的位置:main函数之外
    struct Student
    {
        public int Code;
        public string Name;
        public string Sex;
        public int Old;
        public DateTime Birthday;
    }
    
    定义:声明
    Student s = new Student(); //实例化
    
    赋值:
    s.Code = 1;
    s.Name = "张三";
    s.Sex = "";
    s.Old = 18;
    s.Birthday = Convert.ToDateTime("1999-1-1");
    
    取值:
    s.Code //放在等号左边就是给它赋值,放在右边就是使用它的值

    练习

    请输入学生个数:n
    请输入第1个学生的姓名:
    请输入第1个学生的性别:
    请输入第1个学生的年龄:
    请输入第1个学生的成绩:
    ...
    n
    ****************************
    学生的总成绩为:xxx,平均分为:xxx。
    ****************************
    年龄排名(由大到小):
    姓名:xxx,性别:xxx,年龄:xxx,成绩:xxx。
    ...
    n
    ****************************
    成绩排名(由高到低):
    姓名:xxx,性别:xxx,年龄:xxx,成绩:xxx。
    ...
    n

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 结构体
    {
        class Program
        {
            struct student 
            {
                public string name;
                public string sex;
                public int old;
                public decimal grade;
      
            }
            
            static void Main(string[] args)
            {
                Console.Write("请输入学生的个数:");
                int a =Convert.ToInt32(Console.ReadLine());
                student s = new student();
                ArrayList arr = new ArrayList();
                for (int i = 0; i < a; i++)
                {
    
                    Console.Write("请输入第" + (i + 1) + "个学生的姓名:");
                    s.name = Console.ReadLine();
                    while (true)
                    {
                        if (s.name == " ")
                        {
                            Console.Write("请输入第" +( i + 1) + "个学生的姓名:");
                            s.name = Console.ReadLine();
                        }
                        else if (s.name != " ")
                        {
                            break;
                        }
                    }
                    while (true)
                    {
                        Console.Write("请输入第" + (i + 1) + "个学生的性别:");
                        s.sex = Console.ReadLine();
                        if (s.sex == "" || s.sex == "")
                        {
                            break;
                        }
                    }
                    while (true)
                    {
                        try
                        {
                            Console.Write("请输入第" +( i + 1) + "个学生的年龄:");
                            s.old = Convert.ToInt32(Console.ReadLine());
                            break;
                        }
                        catch
                        {
                        }
                    }
                    while (true) 
                    {
                        try
                        {
                            Console.Write("请输入第" + (i + 1) + "个学生的成绩:");
                            s.grade = Convert.ToInt32(Console.ReadLine());
                            break;
                        }
                        catch
                        {
                        }
                    }
                    arr.Add(s);
                }
                decimal sum = 0;
                //student ss = (student)arr[i];
                for (int i = 0; i < arr.Count; i++) 
                {
                    student so = (student)arr[i];
                    //Console.WriteLine("第" + (i + 1) + "个学生的姓名是:"+ss.name + ",性别是:" + ss.sex + ",年龄是:" + ss.old + ",成绩是:" + ss.grade+"。");
                    sum += so.grade;
                    
                }
    
    
                Console.WriteLine("总分是:" + sum + ",平均分是:" + (sum / a) + "");
    
                //student ss = (student)arr[i];
                //student sss = (student)arr[j];
                for (int i = 0; i < arr.Count; i++) 
                {
                    for (int j= i+1; j < arr.Count; j++)
                    {
                        student ss = (student)arr[i];
                        student sss = (student)arr[j];
                        if (ss.old < sss.old) 
                        {
                            object zhong=arr[i];
                            arr[i]=arr[j];
                            arr[j] = zhong;
    
                        }                
                    }
                
                }
                foreach (student x in arr)
                {
                    
                    Console.WriteLine("姓名是:" + x.name + ",性别是:" + x.sex + ",年龄是:" + x.old + ",成绩是:" + x.grade + "");
                }
                for (int i = 0; i < arr.Count; i++)
                {
                    for (int j = i + 1; j < arr.Count; j++)
                    {
                        student ss = (student)arr[i];
                        student sss = (student)arr[j];
                        if (ss.grade < sss.grade)
                        {
                            object zhong = arr[i];
                            arr[i] = arr[j];
                            arr[j] = zhong;
    
                        }
                    }
    
                }
                foreach (student x in arr)
                {
    
                    Console.WriteLine("姓名是:" + x.name + ",性别是:" + x.sex + ",年龄是:" + x.old + ",成绩是:" + x.grade + "");
                }
    
                    Console.ReadLine();
            }
        }
    }

  • 相关阅读:
    com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: INSTALL_FAILED_UPDATE_INCOMPATIBLE:
    vue 使用video.js循环
    微信小程序使用iconfont
    小程序使用可滑动的tab
    vue cli2引入iconfont
    Couldn't find preset "es2015" relative to directory
    Electron桌面应用框架的使用,结合react项目
    如何用Linux重现《黑客帝国》中的经典界面?
    iOS----------证书的制作
    iOS----------charles如何设置本地映射、和取消本地映射
  • 原文地址:https://www.cnblogs.com/songfengyao/p/5543640.html
Copyright © 2011-2022 走看看