C# Day. 8.5 结构体 枚举 你晕不晕???

今天第二项又进行了结构体,枚举,有点小晕,我得捋顺。。。

   

 

一, 结构体:

1,定义位置:

看图,不多BB:

(1)定义位置位于类的里面,Main函数的外面

struct 结构体名称

{   public   类型    名称;

     ——     ——     ——;

     ………………………………;}    可以定义无数个。

2,声明实例化

还是BB不明白,还得看图:

(1)定义一个集合:(用来存放个体)

集合类型<结构体名称>集合名称=new 集合类型<结构体名称>();

(2)定义单独个体:(每一个个体里面可以由许多内容)

结构体名称  个体名称 = new  结构体名称();

 还得BB几句,就是先创建集合,再创建个体,但是每个个体里面有许多内容(感觉就是集合升级版)。

 

3,赋值

根据所选取的集合类型来选择赋值情况(6种集合,定义集合的时候用的什么类型,就怎么赋值)

 

4,取值

取值和赋值一样,根据集合类型来选择该咋用,不多BB。

 

感觉说不明白,来串代码就明白:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 作业1
 7 {
 8     class Program
 9     {
10         struct student
11         {
12             public string code;
13             public string name;
14             public DateTime birthday;
15             public double score;
16         }
17         static void Main(string[] args)
18         {
19             #region  学生个数
20             Console.Write("请输入学生个数:");
21             Int32 geshu = Convert.ToInt32(Console.ReadLine());
22             #endregion
23             #region  声明实例化   输入学生信息
24             List<student> s = new List<student>();
25             int z = 0;
26             for (int a = 0; a < geshu; a++)
27             {
28                 z = z + 1;
29                 student h = new student();
30                 Console.WriteLine("请输入第" + z + "学生学号:");
31                 h.code = Console.ReadLine();
32                 Console.WriteLine("请输入第" + z + "学生姓名:");
33                 h.name = Console.ReadLine();
34                 Console.WriteLine("请输入第" + z + "学生生日:");
35                 h.birthday = Convert.ToDateTime(Console.ReadLine());
36                 Console.WriteLine("请输入第" + z + "学生成绩:");
37                 h.score = Convert.ToInt32(Console.ReadLine());
38                 s.Add(h);
39             }
40             Console.ReadLine();
41             #endregion
42             #region  学生成绩排序
43             for (int a = 0; a < s.Count - 1; a++)
44             {
45                 for (int b = a + 1; b < s.Count; b++)
46                 {
47                     if (s[a].score < s[b].score)
48                     {
49                         student c = s[a];
50                         s[a] = s[b];
51                         s[b] = c;
52                     }
53                 }
54             }
55             #endregion
56             #region  打印学生成绩
57             Console.WriteLine("---------------学生信息展示------------------");
58 
59             foreach (student h in s)
60             {
61                 int age = DateTime.Now.Year - h.birthday.Year;
62                 string bir = h.birthday.ToString("yyyy年MM月dd日");
63                 Console.WriteLine(h.code + "\t" + h.name + "\t" + bir + "\t" + age + "\t" + h.score);
64             }
65             Console.ReadLine();
66             #endregion
67         }
68     }
69 }

输出结果:

 

没什么好多说的,看看就明白了。

二,枚举

 说一说我对枚举的理解,我认为枚举就是将原本不固定的内容变成固定选择结果的内容,你只能从固定的结果中选择,不能随便乱写了。

上图:

Week 只能选择星期一,二,三,四,五,六,日。感觉就像是单独定义了一个类,但是这个类就只有固定几个结果。

posted on 2017-03-02 21:55  张鑫4477  阅读(240)  评论(0编辑  收藏  举报