#region 练习题一
//使用结构体描述学员的信息,姓名,性别,年龄,班级,专业
//创建两个学员对象,并对其基本信息进行初始化并打印
struct Student
{
public string name;
public bool sex;
public int age;
public int clas;
public string major;
public Student(string name,bool sex, int age, int clas,string major)
{
this.name = name;
this.sex = sex;
this.age = age;
this.clas = clas;
this.major = major;
}
public void Spesk()
{
string str = sex ? "女" : "男";
Console.WriteLine($"姓名:{name},性别:{str},年龄:{age},教室:{clas},专业:{major}");
}
}
#endregion
#region 练习题二
//请简要描述private和public两个关建字的区别
//访问修饰符 用来修饰变量和函数的
//如果默认不写访问修饰符 则为private
//private 私有的 被它修饰的变量和函数 只能在结构体内部使用 外部无法使用
//public 公共的 被它修饰的变量和函数 能在内外部都访问
#endregion
#region 练习题三
//使用结构体藐视矩形的信息,长,宽;创建一个矩形,
//对其长宽进行初始化,并打印矩形的长、宽、面积、周长等信息。
struct Rect
{
public float w;
public float h;
public float area;
public float perimeter;
public Rect(int w,int h)
{
this.w = w;
this.h = h;
area = w * h;
perimeter = 2 * (h + w);
}
public void WriteInfo()
{
Console.WriteLine($"该矩形长为{w},宽为{h},面积为{area},周长为{perimeter}");
}
}
#endregion
#region 练习题四
//使用结构体描述玩家信息,玩家名字,玩家职业
//请用户输入玩家姓名,选择玩家职业,最后打印玩家的攻击信息
//职业:
//战士(技能:冲锋)
//猎人(技能:假死)
//法师(技能:奥术冲击)
//打印结果:猎人唐老师释放了假死
struct PlayerInfo
{
public string name;
public E_occupation occupation;
public PlayerInfo(string name,E_occupation occupation)
{
this.name =name;
this.occupation = occupation;
}
public void Atk()
{
switch (occupation)
{
case E_occupation.Warrior:
Console.WriteLine($"战士{name}释放了冲锋");
break;
case E_occupation.Hunter:
Console.WriteLine($"猎人{name}释放了假死");
break;
case E_occupation.Master:
Console.WriteLine($"法师{name}释放了奥术飞弹");
break;
default:
break;
}
}
}
enum E_occupation
{
/// <summary>
/// 战士
/// </summary>
Warrior,
/// <summary>
/// 猎人
/// </summary>
Hunter,
/// <summary>
/// 法师
/// </summary>
Master,
}
#endregion
#region 练习题五
//使用结构体描述小怪兽
struct Monster
{
public string name;
public int atk;
public Monster(string name)
{
this.name=name;
Random r = new Random();
atk = r.Next(10, 31);
}
public void Atk()
{
Console.WriteLine($"{name}的攻击力是{atk}");
}
}
#endregion
#region 练习题六
//定义一个数组存储10个上面描述的小怪兽,每个小怪兽的名字为(小怪兽+数组下标)
//举例:小怪兽0,最后打印10个小怪兽的名字+攻击力数值
#endregion
#region 练习题七
//应用已学过的知识,实现奥特曼打小怪兽
//提示:
//结构体描述奥特曼与小怪兽
//定义一个方法实现奥特曼攻击小怪兽
//定义一个方法实现小怪兽攻击奥特曼
struct Atm
{
public string name;
public int atk;
public int def;
public int hp;
public Atm(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 -= this.atk - monster.def;
Console.WriteLine($"{name}攻击了{monster.name},造成了{atk - monster.def}伤害,{monster.hp}剩余血量");
}
}
struct Boss
{
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 Atm atm)
{
atm.hp -= this.atk -atm.def;
Console.WriteLine($"{name}攻击了{atm.name},造成了{atk - atm.def}伤害,{atm.hp}剩余血量");
}
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("结构体练习题");
//变量类型[] 数组名 = new 变量类型[10];
//Monster[] monsters = new Monster[10];
//for (int i = 0; i < monsters.Length; i++)
//{
// monsters[i] = new Monster("小怪兽" + i);
// monsters[i].Atk();
//}
Atm a =new Atm("迪迦",10,5,100);
Boss b = new Boss("lalla", 8, 4, 100);
while (true)
{
a.Atk(ref b);
if (b.hp <= 0)
{
Console.WriteLine("奥特曼胜利");
break;
}
b.Atk(ref a);
if (a.hp <= 0)
{
Console.WriteLine("怪兽胜利");
break;
}
Console.WriteLine("按任意键继续");
Console.ReadKey(true);
}
//Student s1 = new Student("1",false,18,1,"计算机");
//Student s2 = new Student("2",true,18,2,"美术");
//s1.Spesk();
//s2.Spesk();
//Rect rect = new Rect(10,10);
//rect.WriteInfo();
//Console.WriteLine("请输入你的姓名");
//string name = Console.ReadLine();
//Console.WriteLine("请输入职业");
//try
//{
// E_occupation o =(E_occupation)int.Parse(Console.ReadLine());
// //根据输入的内容初始化了一个玩家对象
// PlayerInfo info = new PlayerInfo(name,o);
// info.Atk();
//}
//catch
//{
// Console.WriteLine("请输入数字");
//}
}