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?"男":"女"));


 

例:员工信息

namespace ConsoleApplication1
{
    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"
                +"微信:"+(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);


        }
    }
}

例:学生信息

namespace ConsoleApplication1
{
    struct XueSheng
    {
        public int XueHao;
        public string XingMing;
        public double YuWen;
        public double ShuXue;
        public double WaiYu;
        public double ZongFen;
        public int MinCi;
    }
    class Class1
    {
        static void Main(string[] args)
        {
            //int[] a = new int[5];
            XueSheng[] s = new XueSheng[5];
            //输入
            ShuRu(s);
            //排序
            PaiXu(s);
            //填名次
            XieMingCi(s);
            //输出显示
            ShuChu(s);
        }

        private static void ShuChu(XueSheng[] s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                Console.WriteLine(s[i].XueHao + "\t" + s[i].XingMing + "\t" + s[i].YuWen + "\t" + s[i].ShuXue + "\t"
                    + s[i].WaiYu + "\t" + s[i].ZongFen + "\t" + s[i].MinCi);
            }
        }

        private static void XieMingCi(XueSheng[] s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                s[i].MinCi = i + 1;
            }
        }

        private static void PaiXu(XueSheng[] s)
        {
            for (int i = 1; i <= s.Length - 1; i++)
            {
                for (int j = 1; j <= s.Length - i; j++)
                {
                    if (s[j].ZongFen > s[j - 1].ZongFen)
                    {
                        XueSheng temp = s[j];
                        s[j] = s[j - 1];
                        s[j - 1] = temp;
                    }
                }
            }
        }

        private static void ShuRu(XueSheng[] s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                Console.WriteLine("正在输入第" + (i + 1) + "个学生的信息");
                s[i].XueHao = i + 1;
                Console.Write("姓名:");
                s[i].XingMing = Console.ReadLine();
                Console.Write("语文:");
                s[i].YuWen = Convert.ToDouble(Console.ReadLine());
                Console.Write("数学:");
                s[i].ShuXue = Convert.ToDouble(Console.ReadLine());
                Console.Write("外语:");
                s[i].WaiYu = Convert.ToDouble(Console.ReadLine());
                //总分:
                s[i].ZongFen = s[i].YuWen + s[i].ShuXue + s[i].WaiYu;
            }
        }
    }
}

例:对战游戏

namespace ConsoleApplication1
{
    class Class2
    {
        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();

            }

        }
    }
}

 

posted @ 2015-01-06 10:53  五月阳光  阅读(143)  评论(0编辑  收藏  举报