• 结构体的使用规则


    结构体:它是属于用户自定义类型
    定义位置定义在Main函数的外面,类的里面

    定义格式:
    struct 自定义名字
    {
    public 数据类型 名字;
    public 数据类型 名字;
    ...
    ...

    这里可以定义无数个
    }

    声明实例化:
    结构体类型 ss = new 结构体类型();
    Student s = new Student();

    赋值:
    s.名字
    s.名字

    取值:
    s.名字

    下面通过一个例子看看它的定义规则和使用方法

    
    
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 学生
     7 {
     8     class Program
     9     {
    10         struct student                  //自定义student
    11         {
    12             public string numb;
    13             public string name;
    14             public DateTime birthday;
    15             public int score;
    16 
    17 
    18         }
    19 
    20         static void Main(string[] args)
    21         {
    22             Console.Write("请输入录入学生的个数");
    23             int n = Convert.ToInt32(Console.ReadLine());
    24 
    25             List<student> list = new List<student>();
    26           
    27 
    28             for (int i = 1; i <= n; i++)
    29             {
    30                 student s = new student();//声明实例化:  结构体类型 ss = new 结构体类型();
    31                 //赋值
    32                 Console.Write("请输入第" + i + "个学生的学号:");
    33                 s.numb = Console.ReadLine();
    34                 Console.Write("请输入第" + i + "个学生的姓名:");
    35                 s.name = Console.ReadLine();
    36                 Console.Write("请输入第" + i + "个学生的生日:");
    37                 s.birthday = Convert.ToDateTime(Console.ReadLine());
    38                 Console.Write("请输入第" + i + "个学生的成绩:");
    39                 s.score = Convert.ToInt32(Console.ReadLine());
    40 
    41                 list.Add(s);
    42                 if (s.score>100)
    43                 {
    44                     Console.Write("你输入的成绩错误");
    45                 
    46                 }
    47             }
    48              
    49 
    50             Console.WriteLine("===================学生信息展示===========================");
    51             for (int i = 0; i < list.Count; i++)
    52             {
    53                 for (int j = i + 1; j < list.Count; j++)
    54                     if (list[i].score < list[j].score)
    55                     {
    56                         student sss = list[i];
    57                         list[i] = list[j];
    58                         list[j] = sss;
    59                     }
    60             }
    61             foreach (student s in list)
    62             {
    63                 int age = DateTime.Now.Year - s.birthday.Year;
    64                 string sr = s.birthday.ToString("yyyy年MM月dd日");
    65                 Console.WriteLine(s.numb + "	" + s.name + "	" + sr + "	" + age + "	" + s.score);
    66             }
    67 
    68             Console.ReadLine();
    69         }
    70     }
    71 }
    
    
    
    
    
  • 相关阅读:
    [转] JavaScript中的字符串操作
    关于多线程学习的笔记
    [转] jquery 使用方法
    ubuntu下安装tomcat和配置mysql
    [转] 使用CodeViz生成C/C++函数调用关系图
    [转] java中的匿名内部类总结
    [转] Java中继承thread类与实现Runnable接口的区别
    字符串 指针、调试---师傅传授 栈中指针地址 与 堆中数据地址 标准写法
    maven 阿里云节点,速度快
    解决IntelliJ IDEA 创建Maven项目速度慢问题 DarchetypeCatalog
  • 原文地址:https://www.cnblogs.com/zhulijun/p/6501961.html
Copyright © 2020-2023  润新知