C#结构体练习

using System.Collections.Specialized;
using System.Xml.Linq;

namespace  结构体
    {
    struct Player
    {
        public string name;
        public E_occupation occupation;
        
        
        public Player(string name,E_occupation occupation ) 
        {
            this.name = name;
            this.occupation = occupation;
        }

        public void Skill()
        {
            switch (occupation) 
            {
                case E_occupation.Warrior:
                    Console.WriteLine("玩家名称{0},职业战士 ,技能冲锋", name);
                    break;
                case E_occupation.Hunter:
                    Console.WriteLine("玩家名称{0},职业猎人 ,技能假死", name);
                    break;
                case E_occupation.Master:
                    Console.WriteLine("玩家名称{0},职业法师 ,技能奥术冲击", name);
                    break;
            }
        }
       
    }
    enum E_occupation
            {
                Warrior,
                Hunter,
                Master,
            }
    
    class Program {

    static void Main(String[] args)
    {
            string name;
            Console.WriteLine("请输入您的姓名");
            name = Console.ReadLine();
            Console.WriteLine("请选择您的职业0战士 1猎人 2法师");
            Player player=new Player();
            try
            {
                player.occupation = (E_occupation)int.Parse(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("请输入数字");
            }
            player.name = name;
            player.Skill();

          
            
    }
   

    }
}

第二个 小怪兽练习~ ( ~ v ~ )~

using System.Collections.Specialized;
using System.Xml.Linq;

namespace  结构体
    {
    struct Monster
    {
       public int atk; 
       public string name;

        public Monster( string name) 
        {
            this.name = name;
            Random random = new Random();
            atk = random.Next(1, 3);
        }
        public void Atk()
        {
            Console.WriteLine("{0}攻击力{1}",name,atk);
        }
       
    }

    class Program {

    static void Main(String[] args)
    {
            Monster[] monsters= new Monster[10]; 
            for(int i = 0; i < monsters.Length; i++) 
            {
                monsters[i] = new Monster("小怪兽" + i);
                monsters[i].Atk();
            }
    }
   

    }
}

第三个 奥特曼打怪兽练习(^ v ^)

using System.Collections.Specialized;
using System.Threading;
using System.Xml.Linq;

namespace  结构体
    {
    struct OutMan
    {
        public string name;
        public int atk;
        public int def;
        public int hp;

        public OutMan(string name,int atk, int def, int hp)
        {
            this.name = name;
            this.def = def;
            this.hp = hp;
            this.atk = atk;
        }

        public void Atk( ref Monster monster)
        {
            monster.hp -= atk - monster.def;
            Console.WriteLine("{0}攻击了{1}造成了{2},{3}剩余HP{4}", name, monster.name, atk - monster.def, monster.name, monster.hp);
        }
    }

    struct Monster
    {
       public int atk; 
        public int def;
        public int hp;
       public string name;

        public Monster( string name,int atk ,int def,int hp) 
        {
               this .name = name;
               this .def = def;
               this .hp = hp;
            this .atk = atk;
        }
        public void Atk(ref OutMan outMan)
        {
            outMan.hp -= atk - outMan.def;
            Console.WriteLine("{0}攻击了{1}造成了{2},{3}剩余HP{4}", name, outMan.name, atk - outMan.def, outMan.name, outMan.hp);
        }
    }
    class Program {
        static void Main(String[] args)
        {
         OutMan outMan = new OutMan("泰罗奥特曼",8,5,100);
            Monster monster = new Monster("老怪兽", 7, 5, 100);

            while (outMan.hp> 0 && monster.hp>0)
            {
                outMan.Atk(ref monster);
                monster.Atk(ref outMan);
            }
            if (outMan.hp > 0)
            {
                Console.WriteLine("泰罗奥特曼胜利");
            }
            else
            {
                Console.WriteLine("老怪兽胜利");
            }
            
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值