标签:
结构体:
由多种简单类型,组合成一种复杂的类型。使用这种复杂的类型来解决生活中的实际例子。
一、结构体定义:
struct 结构体的名子
{
public 类型名 变量名;
.....
}
struct Student { public string NO; public string Name; public double YuWen; public double ShuXue; public double WaiYu; public double ZongFen; }
二、结构体的使用:
1.使用结构体来定义变量
Student s1 = new Student();
2.给结构体变量赋值。
s1.Name = "";
s1.NO = "";
3.给结构体变量取值。
s2.YuWen
三、复杂结构体。
使用结构体类型的成员变量,来组成更大的结构体。
1.先造个小的结构体
2.使用小的结构体来组合成更大的结构体。
struct Student
{
public string NO;
public string Name;
public ChengJi Score = new ChengJi();
public LianXiFangShi Contact = new LianXiFangShi():
}
struct LianXiFangShi
{
public string DianHua;
public string QQ;
public string YouXiang;
public string ZhuZhi;
}
struct ChengJi
{
public double YuWen;
public double ShuXue;
public double WaiYu;
public double ZongFen;
}
复杂结构体的定义:
Student s1 = new Student();
复杂结构体成员变量的使用。
s1.NO = "s001";
s1.Name = "张三";
s1.Contact.DianHua="18500002222";
s1.Contact.QQ="88888888";
s1.Contact.YouXiang="88888888@qq.com";
s1.Contact.ZhuZhi = "地球";
s1.Score.YuWen=89;
s1.Score.ShuXue=99;
s1.Score.WaiYu=79;
s1.Score.ZongFen = s1.Score.YuWen+s1.Score.ShuXue+s1.Score.WaiYu;
例子:学生的案例
static void Main(string[] args) { Student[] s = new Student[2]; //输入基本信息:学号和姓名 bool a = true; Console.WriteLine("**********第一步:输入学生基本信息**********"); for (int i = 0; i < s.Length; i++) { Console.WriteLine("第" + (i + 1) + "个学生的信息"); Console.WriteLine("学号:"); try { s[i].No = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("姓名:"); s[i].Name = Console.ReadLine(); } catch { Console.WriteLine("错误"); i--; a = false; } } //输入成绩 Console.Clear(); Console.WriteLine("**********第二步:输入学生成绩**********"); for (int i = 0; i < s.Length; i++) { Console.WriteLine("正在输入第" + s[i].Name + "同学的成绩"); Console.Write("语文:"); s[i].Score.YuWen = Convert.ToDouble(Console.ReadLine()); Console.Write("数学:"); s[i].Score.ShuXue = Convert.ToDouble(Console.ReadLine()); Console.Write("外语:"); s[i].Score.WaiYu = Convert.ToDouble(Console.ReadLine()); s[i].Score.ZongFen = s[i].Score.YuWen + s[i].Score.ShuXue + s[i].Score.WaiYu; } //输入联系方式 Console.Clear(); Console.WriteLine("**********第三步:输入联系方式**********"); for (int i = 0; i < s.Length; i++) { Console.WriteLine("正在输入第" + s[i].Name + "同学的联系方式"); Console.WriteLine("电话:"); s[i].Contact.Dianhua = Console.ReadLine(); Console.WriteLine("QQ:"); s[i].Contact.QQ = Console.ReadLine(); Console.WriteLine("邮箱:"); s[i].Contact.YouXiang = Console.ReadLine(); Console.WriteLine("住址:"); s[i].Contact.ZhuZhi = Console.ReadLine(); } //显示学生信息 Console.Clear(); for (int i = 0; i < s.Length; i++) { Console.WriteLine("学号"+s[i].No + "\t姓名:" + s[i].Name + "\t总分" + s[i].Score.ZongFen + "\t电话" + s[i].Contact.Dianhua); } }
标签:
原文地址:http://www.cnblogs.com/languang/p/4535927.html