c#结构体练习题 |
您所在的位置:网站首页 › 雪花晶的功效与作用是什么样的 › c#结构体练习题 |
1.//使用结构体描述学员信息,姓名,性别,年龄,班级,专业 //创建两个学员对象,并且对基本信息进行初始化并打印 public struct Student { public string name; public string sex; public int age; public int grade; string major; public void Print() { Console.WriteLine("学生名字是{0},性别是{1},年龄是{2},班级是{3}", name, sex, age, grade); } } static void Main(string[] args) { Student s1=new Student(); s1.age = 10; s1.name = "张三"; s1.grade = 1; s1.sex = "女"; s1.Print(); Student s2=new Student (); s2.age = 12; s2.name = "张si"; s2.grade = 2; s2.sex = "男"; s2.Print(); } } } 2.//使用结构体描述矩形信息,长宽,创建一个矩形 //对其长宽进行初始化,并打印矩形的长,宽,面积,周长信息 public struct Tangle { int chang; int kuan; public Tangle (int chang,int kuan) { this.chang = chang; this.kuan = kuan; } public void Print() { int area=chang *kuan ; int zhouChang=2*(chang +kuan ); Console.WriteLine("长是{0},宽是{1},面积是{2},周长是{3}",chang ,kuan,area,zhouChang ); } } static void Main(string[] args) { Tangle tangle = new Tangle(10, 5); tangle.Print(); } } 3. //使用结构体描述玩家信息,玩家名字,玩家职业 //请用户输入玩家姓名,选择玩家职业,最后打印玩家的攻击信息 //职业: //战士:技能(冲锋) //猎人:技能(假死) //法师:技能(法术) //打印结果:猎人小米释放了假死 public struct UserInformaton{ public string name; public E_profession e_Profession; string jiNeng; public UserInformaton (string name,E_profession e_Profession,string jiNeng) { this.name = name; this.e_Profession = e_Profession; this.jiNeng = jiNeng; } public void Print() { Console.WriteLine("{0}{1}释放了{2}",e_Profession,name ,jiNeng ); } } public enum E_profession { worrior, hunter, magic, } static void Main(string[] args) { string jiNeng = ""; Console.WriteLine("请输入玩家姓名"); string name = Console.ReadLine(); Console.WriteLine("请输入玩家职业,输入0为战士,1为猎人,2为法师"); E_profession e_Profession = (E_profession)int.Parse(Console.ReadLine()); switch (e_Profession) { case E_profession.worrior: jiNeng = "冲锋"; break; case E_profession.hunter: jiNeng = "假死"; break; case E_profession.magic: jiNeng = "法术"; break; } UserInformaton userInformaton = new UserInformaton(name ,e_Profession,jiNeng ); userInformaton.Print(); } } } 4.//定义一个小怪兽 //定义一个数组存储10个上面描述的小怪兽,每个小怪兽名字为{小怪兽+数组下标} //举例:小怪兽0,最后打印10个小怪兽名字+攻击力数值 class Program { struct Monster { string name; int atk; public Monster(string name) { this.name = name; Random r = new Random(); atk = r.Next(2, 20); } public void Print() { Console.WriteLine("{0}怪兽攻击力是{1}",name ,atk ); } } static void Main(string[] args) { Monster[] monsters = new Monster[10]; for (int i=0;i public string name; public int atk; public int def; public int hp; public Boss(string name, int atk, int def, int hp) { this.name = name; this.atk = atk; this.def = def; this.hp = hp; } public void Atk(ref OutMan outMan) { outMan.hp -= atk - outMan.def; Console.WriteLine("{0}攻击了{1},造成了{2}伤害,剩余血量{3}", name, outMan.name, atk - outMan.def, outMan.hp); } } 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.atk = atk; this.def = def; this.hp = hp; } public void Atk(ref Boss monster) { monster.hp -= atk - monster.def; Console.WriteLine("{0}攻击了{1},造成了{2}伤害,剩余血量{3}", name, monster .name, atk - monster .def, monster .hp); } } static void Main(string[] args) { OutMan outMan = new OutMan("奥特曼", 10, 5, 100); Boss boss = new Boss("哥斯拉", 8, 4, 100); while (true) { outMan.Atk(ref boss ); if (boss .hp Console.WriteLine("奥特曼胜利"); break; } Console.WriteLine("按任意键继续"); Console.ReadKey(true); } } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |