C#结构体的使用
结构体:相当于是我们自己定义的一种复杂的类型。
常见简单类型: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+"\t"+zhangsan.Name+"\t"+zhangsan.Age);
Console.WriteLine(zhangsan.Nation+"\t"+(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+"\t"+zhangsan.Name+"\t"+zhangsan.Age);
Console.WriteLine(zhangsan.Nation+"\t"+(zhangsan.Sex?"男":"女"));
Console.WriteLine("联系方式:");
Console.WriteLine(
"QQ:"+(zhangsan.LianXi.QQ==null?"无":zhangsan.LianXi.QQ)+"\t"//若没输入qq,打印“无”
+"微信:"+(zhangsan.LianXi.WeiXin == null?"无":zhangsan.LianXi.WeiXin)+"\t"
+"手机:"+(zhangsan.LianXi.Telephone==null?"无":zhangsan.LianXi.Telephone)+"\t"
+"邮箱:"+(zhangsan.LianXi.Email==null?"无":zhangsan.LianXi.Email)+"\t"
+"地址:"+zhangsan.LianXi.Address+"\t"+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 + "\t生命力:" + r1.Blood+"\t攻击力:"+r1.Attack+"\t防御力:"+r1.Defend+"\t敏捷度:"+r1.Quick);
Console.WriteLine("VS");
Console.WriteLine("姓名:" + r2.Name + "\t生命力:" + r2.Blood + "\t攻击力:" + r2.Attack + "\t防御力:" + r2.Defend + "\t敏捷度:" + 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 + "\t");
Console.Write("姓名:" + r2.Name + "生命力:" + r2.Blood);
Console.ResetColor();
Console.WriteLine();
Console.WriteLine();
}
}
练习1.输入学生成绩信息
struct Student
{
public string Name;
public int No;
public double ToltallGrd;
public double Chinese;
public double math;//快速移至下行尾
}
//输入学生成绩信息并排序
static void Main(string[] args)
{
Student[] a = new Student[5];
//输入成绩
GetGrade(a);
//根据总分排序
Order(a);
//输出学生成绩信息
Output(a);
}
private static void Output(Student[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("姓名:{0}\t学号:{1}\t名次:\t{2}总分:{3}\t语文:{4}\t数学:{5}",
a[i].Name, a[i].No, i + 1, a[i].ToltallGrd, a[i].Chinese, a[i].math);
}
}
private static void Order(Student[] a)
{
for (int i = 1; i <= a.Length - 1; i++)
{
for (int j = 1; j <= a.Length - i; j++)
{
if (a[j].ToltallGrd > a[j - 1].ToltallGrd)
{
Student tem = a[j];
a[j] = a[j - 1];
a[j - 1] = tem;
}
}
}
}
private static void GetGrade(Student[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("请输入第" + (i + 1) + "名学生姓名:");
a[i].Name = Console.ReadLine();
a[i].No = i + 1;
Console.Write("语文:");
a[i].Chinese = Convert.ToDouble(Console.ReadLine());
Console.Write("数学:");
a[i].math = Convert.ToDouble(Console.ReadLine());
a[i].ToltallGrd = a[i].Chinese + a[i].math;
}
}
练习2.米罗vs沙加
struct Roal
{
public int blood;
public int def;
public int mov1;
public int mov2;
}
class 对战游戏
{
static void Main(string[] args)
{
Roal cast1, cast2;
//角色1的属性
cast1.blood = 1000;
cast1.def = 50;
cast1.mov1 = 120;
cast1.mov2 = 160;
//角色2的属性
cast2.blood = 1000;
cast2.def = 60;
cast2.mov1 = 110;
cast2.mov2 = 130;
//颜色设置
Console.ForegroundColor = ConsoleColor.Blue;
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
//输入操作
//角色1攻击
while(cast1.blood >0&&cast2.blood >0)
{
Console.WriteLine("请米罗发招:a-天蝎意念波 s-猩红毒针");
string m = Console.ReadLine();
Random n1 = new Random();
if (m == "a")
{
int s = n1.Next(60);//角色2的防御值
int s2 = cast1.mov1 - s;//攻击效果
cast2.blood -= s2;//角色2血量减少
}
else if (m == "s")
{
int s = n1.Next(60);//角色2的防御值
int s2 = cast1.mov2 - s;//攻击效果
cast2.blood -= s2;//角色2血量减少
}
//角色2攻击
Console.WriteLine("请沙加发招:k-天舞宝轮 l-六道轮回");
string h= Console.ReadLine();
Random i = new Random();
if (h == "k")
{
int s = i.Next(50);//角色1的防御值
int s2 = cast1.mov1 - s;//攻击效果
cast1.blood -= s2;//角色1血量减少
}
else if (h == "l")
{
int s = n1.Next(50);//角色1的防御值
int s2 = cast1.mov2 - s;//攻击效果
cast1.blood -= s2;//角色1血量减少
}
else
{
Console.WriteLine("发招失败");
}
Console.Clear();
cast1.blood = cast1.blood < 0 ? 0 : cast1.blood ;//控制血量不要小于零
cast2.blood = cast2.blood < 0 ? 0 : cast2.blood;//控制血量不要小于零
Console.WriteLine ("米罗血量:{0}",cast1.blood );
Console.WriteLine("沙加血量:{0}",cast2.blood);
}
//输出对殴结果
if (cast1.blood == 0 && cast2.blood == 0)
{
Console.WriteLine("同归于尽");
}
else if (cast1.blood ==0)
{
Console.WriteLine("沙加打败米罗");
}
else
{
Console.WriteLine("米罗打败沙加,成为最强黄金圣斗士");
}
}