结构体:相当于是我们自己定义的一种复杂的类型。
常见简单类型:int... double float bool char string
常见复杂类型:DateTime 数组类型
生活中大部份的对象都是复合型的对象。
如何定义结构体类型?
一般来说结构体的定义要放在class的外面或class的里面,尽量不放在Main的里面。
struct 自定义类型名
{
public 变量类型 变量名;
......;
......;
......;
}
例如:
struct YuanGong //自定义的数据类型。用来描述员工的信息。
{
public string NO;
public string Name;
public int Age;
public string Nation;
public bool Sex;
}
如何用自定义的类型来定义变量?
自定义类型名 变量 = new 自定义类型名();
如何使用自定义类型的变量?
变量.子变量 = "xxxx";
Console.WriteLine(变量名.子变量);
例如:
//定义自定义类型的变量
YuanGong zhangsan = new YuanGong();
//给变量赋值
zhangsan.NO = "Y001";
zhangsan.Name = "张三";
zhangsan.Age = 22;
zhangsan.Sex = true;
zhangsan.Nation = "汉族";
//对变量取值
Console.WriteLine(zhangsan.NO+" "+zhangsan.Name+" "+zhangsan.Age);
Console.WriteLine(zhangsan.Nation+" "+(zhangsan.Sex?"男":"女"));
例题1.描述员工信息
struct YuanGong //自定义的数据类型。用来描述员工的信息。 { public string NO; public string Name; public int Age; public string Nation; public bool Sex; public LianXiFangShi LianXi; } struct LianXiFangShi { public string QQ; public string WeiXin; public string Email; public string Telephone; public string Address; public string ZipCode; } class Program { static void Main(string[] args) { YuanGong zhangsan = new YuanGong(); zhangsan.NO = "Y001"; zhangsan.Name = "张三"; zhangsan.Age = 22; zhangsan.Sex = true; zhangsan.Nation = "汉族"; zhangsan.LianXi.QQ = "434354546"; //zhangsan.LianXi.WeiXin = "张三三"; //zhangsan.LianXi.Email = "zhangsan@tom.com"; zhangsan.LianXi.Address = "张店区张家胡同"; zhangsan.LianXi.ZipCode = "25000"; zhangsan.LianXi.Telephone = ""; YuanGong lisi = new YuanGong(); lisi.NO = "Y002"; lisi.Name = "李四"; lisi.Age = 25; lisi.Sex =false; lisi.Nation = "回族"; Console.WriteLine("**********张三的个人信息***********"); Console.WriteLine(zhangsan.NO+" "+zhangsan.Name+" "+zhangsan.Age); Console.WriteLine(zhangsan.Nation+" "+(zhangsan.Sex?"男":"女")); Console.WriteLine("联系方式:"); Console.WriteLine( "QQ:"+(zhangsan.LianXi.QQ==null?"无":zhangsan.LianXi.QQ)+" "//若没输入qq,打印“无” +"微信:"+(zhangsan.LianXi.WeiXin == null?"无":zhangsan.LianXi.WeiXin)+" " +"手机:"+(zhangsan.LianXi.Telephone==null?"无":zhangsan.LianXi.Telephone)+" " +"邮箱:"+(zhangsan.LianXi.Email==null?"无":zhangsan.LianXi.Email)+" " +"地址:"+zhangsan.LianXi.Address+" "+zhangsan.LianXi.ZipCode); }
例题、两人对战游戏
struct Ren { public string Name; public int Blood; public int Attack; public int Defend; public int Quick; } struct WuGong { public string Name; public int Attack; } static void Main(string[] args) { WuGong[] wg = new WuGong[3]; wg[0].Name = "辟邪剑法"; wg[0].Attack = 500; wg[1].Name = "降龙十八掌"; wg[1].Attack = 600; wg[2].Name = "暗然消魂掌"; wg[2].Attack = 400; Ren r1 = new Ren(); Ren r2 = new Ren(); //初化两个战斗人员的属性。 Console.Write("请输入第一个战士姓名:"); r1.Name = Console.ReadLine(); Console.Write("请输入第二个战士姓名:"); r2.Name = Console.ReadLine(); //生成血量 Random rand = new Random(); r1.Blood = rand.Next(1000) + 1000; r2.Blood = rand.Next(1000) + 1000; //生成攻防 r1.Attack = rand.Next(50) + 50; r2.Attack = rand.Next(50) + 50; r1.Defend = rand.Next(50) + 50; r2.Defend = rand.Next(50) + 50; //生成身法 r1.Quick = rand.Next(100); r2.Quick = rand.Next(100); Console.WriteLine("姓名:" + r1.Name + " 生命力:" + r1.Blood+" 攻击力:"+r1.Attack+" 防御力:"+r1.Defend+" 敏捷度:"+r1.Quick); Console.WriteLine("VS"); Console.WriteLine("姓名:" + r2.Name + " 生命力:" + r2.Blood + " 攻击力:" + r2.Attack + " 防御力:" + r2.Defend + " 敏捷度:" + r2.Quick); Console.WriteLine("按任意键开始..."); Console.ReadKey(); while(true) { //跳出循环。 if(r1.Blood <=0 && r2.Blood<=0) { Console.WriteLine(r1.Name+"与"+r2.Name+"同归于尽了!"); break; } if(r1.Blood<=0) { Console.WriteLine(r2.Name+"把"+r1.Name+"KO了~!"); break; } if (r2.Blood <= 0) { Console.WriteLine(r1.Name + "把" + r2.Name + "KO了~!"); break; } //对战 //大招 int dz1 = rand.Next(10); //r2的大招 if (dz1 >= 8) //大招 { WuGong w1 = wg[rand.Next(3)]; int b1 = rand.Next(100); //r1失掉的血 int gj1 = rand.Next(100) - 50; //为了浮动r1的攻击 int fy1 = rand.Next(100) - 50;//为了浮动r2的防御 b1 = (b1 + (r2.Attack + gj1+w1.Attack) - (r1.Defend + fy1)) < 0 ? 0 : (b1 + (r2.Attack + gj1+w1.Attack) - (r1.Defend + fy1)); r1.Blood -= b1; if (r1.Blood < 0) { r1.Blood = 0; } //稍待一下。 System.Threading.Thread.Sleep(2000); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(r2.Name + "使用大招"+w1.Name+"发起攻击," + r1.Name + "失掉生命力" + b1 + "点!"); Console.ResetColor(); Console.WriteLine(); } else //平常招式 { int sf1 = rand.Next(80); if (r1.Quick - sf1 >= 0) { Console.WriteLine(r1.Name + "躲过了" + r2.Name + "攻击"); } else { int b1 = rand.Next(100); //r1失掉的血 int gj1 = rand.Next(100) - 50; //为了浮动r1的攻击 int fy1 = rand.Next(100) - 50;//为了浮动r2的防御 b1 = (b1 + (r2.Attack + gj1) - (r1.Defend + fy1)) < 0 ? 0 : (b1 + (r2.Attack + gj1) - (r1.Defend + fy1)); r1.Blood -= b1; if (r1.Blood < 0) { r1.Blood = 0; } //稍待一下。 System.Threading.Thread.Sleep(2000); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(r2.Name + "发起攻击," + r1.Name + "失掉生命力" + b1 + "点!"); Console.ResetColor(); Console.WriteLine(); } } //稍待一下。 System.Threading.Thread.Sleep(2000); int dz2 = rand.Next(10); //r2的大招 if (dz2 >= 8) //大招 { WuGong w1 = wg[rand.Next(3)]; int b2 = rand.Next(100); //r1失掉的血 int gj1 = rand.Next(100) - 50; //为了浮动r1的攻击 int fy1 = rand.Next(100) - 50;//为了浮动r2的防御 b2 = (b2 + (r1.Attack + gj1 + w1.Attack) - (r2.Defend + fy1)) < 0 ? 0 : (b2 + (r1.Attack + gj1 + w1.Attack) - (r2.Defend + fy1)); r2.Blood -= b2; if (r2.Blood < 0) { r2.Blood = 0; } //稍待一下。 System.Threading.Thread.Sleep(2000); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(r1.Name + "使用大招" + w1.Name + "发起攻击," + r2.Name + "失掉生命力" + b2 + "点!"); Console.ResetColor(); Console.WriteLine(); } else { int sf2 = rand.Next(80); if (r2.Quick - sf2 >= 0) { Console.WriteLine(r2.Name + "躲过了" + r1.Name + "攻击"); } else { int b2 = rand.Next(100);//r2失掉的血 int gj2 = rand.Next(100) - 50; //为了浮动r1的攻击 int fy2 = rand.Next(100) - 50;//为了浮动r2的防御 b2 = (b2 + (r1.Attack + gj2) - (r2.Defend + fy2)) < 0 ? 0 : (b2 + (r1.Attack + gj2) - (r2.Defend + fy2)); r2.Blood -= b2; if (r2.Blood < 0) { r2.Blood = 0; } Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine(r1.Name + "发起攻击," + r2.Name + "失掉生命力" + b2 + "点!"); Console.ResetColor(); Console.WriteLine(); } } Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("姓名:" + r1.Name + "生命力:" + r1.Blood + " "); Console.Write("姓名:" + r2.Name + "生命力:" + r2.Blood); Console.ResetColor(); Console.WriteLine(); Console.WriteLine(); } }