结构体:自定义类型 值类型
一组变量的组合
需要定义的位置 class里面 main函数外面
里面包含的变量可以是多种数据类型的
例如学生信息的结构体:学号,姓名,性别,分数
struct Student { public int xuehao; public string name; public string sex; public double score; } //实例化结构体 //Student st = new Student(); //st.xuehao = 1001; //st.name = "张三"; //st.sex = "男"; //st.score = 33; //Student st1 = new Student(); //st1.xuehao = 1002; //st1.name = "李四"; //st1.sex = "女"; //st1.score = 44;
还可以把分数再细分为语文成绩,数学成绩,英语成绩
struct Student { public int xuehao; public string name; public string sex; public Score score; } struct Score { public double yufen; public double shufen; public double yingfen; } //ArrayList al = new ArrayList(); //Console.Write("请输入班级人数:"); //int a = int.Parse(Console.ReadLine()); //for (int i = 0; i < a;i++ ) //{ // Student sst = new Student(); // Console.Write("请输入第{0}个学生的学号:",(i+1)); // sst.xuehao = int.Parse(Console.ReadLine()) ; // Console.Write("请输入第{0}个学生的姓名:", (i + 1)); // sst.name = Console.ReadLine(); // Console.Write("请输入第{0}个学生的性别:", (i + 1)); // sst.sex = Console.ReadLine(); // Console.Write("请输入第{0}个学生的分数:", (i + 1)); // sst.score = double.Parse(Console.ReadLine()); // al.Add(sst); //} //Console.WriteLine("所有人员信息输入完毕!请按回车键开始打印!"); //Console.ReadLine(); //for (int i = 0; i < al.Count;i++ ) //{ // Student sst = (Student)al[i]; // Console.WriteLine("第{0}个学生的学号是:{1},姓名是{2},性别是{3},分数是{4}。",(i+1),sst.xuehao,sst.name,sst.sex,sst.score); //} ////实例化 //Student st = new Student(); //st.score.yufen = 77; //st.score.shufen = 88; //st.score.yingfen = 99;
时间: 2024-10-06 12:34:20