C#結構體的使用

C#結構體的使用

捕獲5

結構體:至關因而咱們本身定義的一種複雜的類型。

常見簡單類型:int...  double float bool char string  數組

常見覆雜類型:DateTime  數組類型微信

生活中大部份的對象都是複合型的對象。dom

如何定義結構體類型?

通常來講結構體的定義要放在class的外面或class的裏面,儘可能不放在Main的裏面。
struct 自定義類型名
{
    public 變量類型  變量名;
    ......;
    ......;
    ......;ui

}
例如:
    struct YuanGong  //自定義的數據類型。用來描述員工的信息。
    {
        public string NO;
        public string Name;
        public int Age;
        public string Nation;
        public bool Sex;
    }spa

如何用自定義的類型來定義變量?

自定義類型名 變量 = new 自定義類型名();code

如何使用自定義類型的變量?
變量.子變量 = "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.描述員工信息blog

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);


        }

捕獲9

 

例題、兩人對戰遊戲排序

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();

            }

        }

捕獲8

練習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;

            }
        }

捕獲12

練習2.郭靖PK歐陽克

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("郭靖戰勝歐陽克,和黃蓉去了桃花島");
            }


        }
捕獲6
相關文章
相關標籤/搜索