C#类 构造方法

using System;

namespace ClassHomework

{

    //英雄类

    class Hero {

        //名字

        public string heroName;

        //血量

        public int heroHp;

        //攻击力

        public int heroAttack;

        //方法1:给英雄每个字段赋值

        public void InitHero(string _name,int _hp,int _attack) {

            heroName = _name;

            heroHp = _hp;

            heroAttack = _attack;

        }

        //方法2:描述英雄信息

        public void DescriptHero() {

            Console.WriteLine("英雄{0},血量为{1},攻击力为{2}",this.heroName,this.heroHp,this.heroAttack);

        }

    }

    class Math {

        //1、求三个数中最大值

        public int FindMaxNumWith(int a,int b,int c) {

            //int max = a > b ? a : b;

            //max = max > c ? max : c;

            int max = 0;

            if (a > b)

            {

                max = a;

            }

            else {

                max = b;

            }

            if (max<c)

            {

                max = c;

            }

            return max;

        }

        //2、输出数组

        public void PrintArray(int[] arr) {

            for (int i = 0; i < arr.Length; i++)

            {

                Console.Write(arr[i]+" ");

            }

        }

        //3、随机一个数组

        public int[] RandomArray(int min, int max, int length) {

            Random random = new Random();

            int[] arr = new int[length];

            for (int i = 0; i < length; i++)

            {

                int num = random.Next(min,max+1);

                arr[i] = num;

            }

            return arr;

        }

        //4、排序数组

        public void SortArray(int[] arr, bool isAsc) {

            for (int i = 0; i < arr.Length-1; i++)

            {

                for (int j = 0; j < arr.Length-1-i; j++)

                {

                    if (isAsc)

                    {

                        if (arr[j] > arr[j + 1])

                        {

                            int temp = arr[j];

                            arr[j] = arr[j + 1];

                            arr[j + 1] = temp;

                        }

                    }

                    else {

                        if (arr[j] < arr[j + 1])

                        {

                            int temp = arr[j];

                            arr[j] = arr[j + 1];

                            arr[j + 1] = temp;

                        }

                    }

                }

            }

        }

    }

    //小怪类

    class Monster {

        public string monsterName;

        public int monsterHp;

        public int monsterAttack;

        //防御值

        public int defenseValue = 100;

        //初始化数据

        public void InitMonster(string _name,int _hp,int _attack)

        {

            monsterName = _name;

            monsterHp = _hp;

            monsterAttack = _attack;

        }

        //描述信息

        public void DescriptMonster() {

            Console.WriteLine("小怪{0},血量为{1},攻击力为{2}",

                this.monsterName,this.monsterHp,this.monsterAttack);

        }

        //收到攻击

        public void BeAttacked(int damage) {

            if (this.monsterHp >= 5000)

            {

                this.defenseValue = 100;

            }

            else if (this.monsterHp >= 3000 && this.monsterHp < 5000)

            {

                //this.monsterHp + damage - this.defenseValue: 为了模拟

                //上次没有收到攻击时是否是5000以上的临界值

                if (this.monsterHp + damage - this.defenseValue >= 5000)

                {

                    Console.WriteLine("防御减半!!!!!!!!!!!");

                }

                this.defenseValue = 50;

            }

            else {

                if (this.monsterHp + damage - this.defenseValue >=3000)

                {

                    Console.WriteLine("防御丢失!!!!!!!!!");

                }

                this.defenseValue = 0;

            }

            //抵挡一部分伤害值

            damage -= this.defenseValue;

            //让小怪当前的血量减去伤害值

            this.monsterHp -= damage;

            Console.WriteLine("{0}受到了{1}的伤害",this.monsterName,damage);

            if (this.monsterHp <= 0)

            {

                //小怪死亡

                this.Death();

                this.monsterHp = 0;

            }

           this.ShowHp();

        }

        //返回血量

        public void ShowHp() {

            Console.WriteLine("当前血量为:{0}",this.monsterHp);

        }

        //小怪死亡

         void Death() {

            Console.WriteLine("啊啊啊啊啊,我死了");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            //1、创建一个英雄

            //Hero hero1 = new Hero();

            //hero1.InitHero("提莫",800,80);

            //hero1.DescriptHero();

            //Hero hero2 = new Hero();

            //hero2.InitHero("凯南",900,90);

            //hero2.DescriptHero();

            //2

            //Math math = new Math();

            //求出三个数最大值

            //int max =  math.FindMaxNumWith(5,9,11);

            //Console.WriteLine("最大值为:"+max);

            //得到一个随机数组,1-99,长度10

            //int[] arr = math.RandomArray(1,99,10);

            //打印一下arr

            //Console.WriteLine("随机数组为:");

            //math.PrintArray(arr);

            //从小到大排序数组arr

            //math.SortArray(arr,true);

            //Console.WriteLine("\n排序后数组为:");

            //math.PrintArray(arr);

            //创建小怪

            Monster monster1 = new Monster();

            monster1.InitMonster("纳什男爵",10000,200);

            monster1.DescriptMonster();

            //当小怪血量大于0,一直收到伤害

            int times = 0;

            while (monster1.monsterHp > 0) {

                times++;

                Console.WriteLine("第{0}次攻击!",times);

                monster1.BeAttacked(299);

            }

         

            Console.ReadKey();

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值