• 结构体,枚举类型


    结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样。

    一、定义的方法:

    struct student

    {

    public int nianling;

    public int fenshu;

    public string name;

    public string sex;

    public int sum;

    }

    以上的语句就是定义一个名称为student的结构体,其中包含int类型的年龄、分数、总和,和string类型的姓名、性别。

    二、用法:

    在main主函数外面定义了一个名称为student的结构体,以便于main函数之中使用。

    student st = new student();//这句话是在main函数之中定义了一个名为st的student类型的结构体。

    下面开始为里面的每个元素赋值:(结构体名+点+结构体里面的变量名称=值)

    main函数下

    {

    st.nianling=22;

    st.fenshu=80;

    st.name="小李";

    }

    赋值完成之后可以打印出被赋值的项。

    三、结构体类型里面包含结构体类型:

    可以在此前的student的结构体中在定义一个结构体

    public shuxing sx;//代表一个shuxing结构体变量组
    }
    public struct shuxing
    {
    public double tizhong;
    public double shengao;
    public int nianling;
    public string hunfou;
    }

    这样就可以在用的时候省下再次初始化结构体。

    public struct student//如果想让其他添加出来的类也能够使用此结构体,需要在前面加上public
    {
    public int nianling;//想让其他的类可以访问到其中的变量需要加上public
    public string name;
    public string sex;
    public One qq;//可以结构体中包含另一个结构体
    public string[] shuzu;//可以直接定义一个数组,但是没有开辟空间
    }
    public struct One
    {
    public string nb;
    public string abc;
    }
    static void Main(string[] args)
    {

    #region
    //为里面的每个元素赋值:(结构体名+点+结构体里面的变量名称=值)
    student st = new student();//使用之前需要先初始化一下
    st.name = "张三";//初始化出来的变量名可以看做一个类对象
    st.nianling = 21;//类对象的名称是不能相同的
    st.sex = "男";
    st.name = "王五";
    //使用的时候利用变量名点出来其中的变量进行使用
    Console.WriteLine(st.name);
    st.qq.abc="qsqs";//结构体中包含另一个结构体类型,可以直接点出来一以下的变量
    st.shuzu = new string [9];//使用之前需要先开辟空间
    st.shuzu[0] = "赵六";//数组元素赋值方式

    student st1 = new student();//可以多次初始化类,注意不同的变量名
    st1.name = "李四";
    st1.nianling = 22;
    st1.sex = "女";
    #endregion
    }

    枚举类型:
    1.枚举类型之针对字符串,对于索引,无意义
    2.常量的集合,这些常量只能取值,不能赋值
    3.用常量表示所引用的字符串,这样可以省去重复写入长字符串

    注意:枚举类型和结构体都属于值类型。

    练习:
    20人投票,五个候选人,用switch case

    //20人投票 switch case 枚举
    //投票的时候输入1,2,3,4,5
    //利用12345来确定是哪一个候选人得票
    //计算得票数
    //得票最高的胜出

    Console.WriteLine("请输入1,2,3,4,5来分别代表张三,李四,王五,赵六,冯七");
    int a=0;int b=0;int c=0;int d=0;int e=0;int f = 0;
    for (int i = 1; i < 21;i++ )
    {
    Console.Write("第"+i+"位同学的选票:");
    int n = int.Parse(Console.ReadLine());
    switch (n)
    {
    case(int )h.one:



    a+=1;
    break;
    case(int)h.two:

    b+=1;
    break;
    case(int)h.three:

    c+=1;
    break;
    case(int)h.four:

    break;
    d+=1;
    case(int)h.five:

    e+=1;
    break;
    default:

    f += 1;
    break;

    }
    }
    if(a>b&&a>c&&a>d&&a>e)
    {
    Console.WriteLine("张三当选!票数是:"+a);
    }
    else if (b > a && b > c && b > d && b > e)
    {
    Console.WriteLine("李四当选!票数是:" + b);
    }
    else if (c > b && c > a && c > d && c > e)
    {
    Console.WriteLine("王五当选!票数是:" + c);
    }
    else if (d > b && d > c && d > a && d > e)
    {
    Console.WriteLine("赵六当选!票数是:" + d);
    }
    else if (e > b && e > c && e > d && e > a)
    {
    Console.WriteLine("冯七当选!票数是:" + e);
    }
    Console.WriteLine("弃权的票数是:"+f);
    Console.ReadLine();

  • 相关阅读:
    TestNg线程池配置、执行次数配置、超时配置
    testng.xml文件结构组成及节点属性说明
    ReportNg 测试报告的定制修改【转】
    TestNg依赖详解(三)------灵活的文件配置依赖
    TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)
    TestNg依赖配置基础用法(单一方法依赖)------TestNg依赖详解(一)
    compareTo,Comparator和equals
    HashMap源码解析
    redis的相关知识
    IO模型
  • 原文地址:https://www.cnblogs.com/dulovexin/p/4962932.html
Copyright © 2020-2023  润新知