Files
Csharp/C#基础/Lesson12_结构体练习/Program.cs
T
2025-09-30 16:49:43 +08:00

179 lines
4.7 KiB
C#

using System;
using System.Threading;
namespace Lesson
{
struct Member
{
public string name;
public bool sex;
public int age;
public string clas;
public string major;
public Member(string name, bool sex, int age, string clas, string major)
{
this.name = name;
this.sex = sex;
this.age = age;
this.clas = clas;
this.major = major;
}
public void PrintInfo()
{
Console.WriteLine("学员信息:");
Console.WriteLine($"姓名:{name}");
Console.WriteLine($"性别:{(sex ? "" : "")}"); // 将bool转换为中文
Console.WriteLine($"年龄:{age}");
Console.WriteLine($"班级:{clas}");
Console.WriteLine($"专业:{major}");
}
}
struct PlayerInfo
{
public string name;
public E_Occupation occupation;
public PlayerInfo(string name,E_Occupation occupation)
{
this.name = name;
this.occupation = occupation;
}
public void PrintInfo()
{
Console.WriteLine("玩家攻击信息");
switch (occupation)
{
case E_Occupation.Warrior:
Console.WriteLine($"{occupation}{name}释放了冲锋");
break;
case E_Occupation.Hunter:
Console.WriteLine($"{occupation}{name}释放了假死");
break;
case E_Occupation.Mage:
Console.WriteLine($"{occupation}{name}释放了奥数冲击");
break;
}
}
}
struct Monster
{
public string name;
public int atk;
public Monster(string name)
{
this.name = name;
Random r = new Random();
atk = r.Next(10,30);
}
public void Atk()
{
Console.WriteLine("{0}的攻击力为{1}",name,atk);
}
}
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 -= atk - monster.def;
Console.WriteLine("{0}攻击了{1}造成了{2}点伤害,{3}剩余{4}HP", name, monster.name, atk - monster.def,monster.name, 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 -= atk - atm.def;
Console.WriteLine("{0}攻击了{1}造成了{2}点伤害,{3}剩余{4}HP", name, atm.name, atk - atm.def,atm.name, atm.hp);
}
}
enum E_Occupation
{
/// <summary>
/// 战士
/// </summary>
Warrior,
/// <summary>
/// 猎人
/// </summary>
Hunter,
/// <summary>
/// 法师
/// </summary>
Mage
}
class Program
{
static void Main(string[] args)
{
Member m1 = new Member("123",true,18,"三年一班","计算机");
m1.PrintInfo();
Member m2 = new Member("321", false, 18, "一年三班", "算计机");
m2.PrintInfo();
PlayerInfo p1 = new PlayerInfo("HK", E_Occupation.Warrior);
p1.PrintInfo();
Monster[] monsters = new Monster[10];
for (int i = 0; i < monsters.Length; i++)
{
monsters[i] = new Monster("小怪兽"+i);
monsters[i].Atk();
}
Atm atm = new Atm("雷欧奥特曼",10,5,100);
Boss boss = new Boss("哥斯拉",8,4,100);
while (true)
{
atm.Atk(ref boss);
boss.Atk(ref atm);
if (boss.hp <= 0)
{
Console.WriteLine("你赢了");
break;
}
if (atm.hp <= 0)
{
Console.WriteLine("你输了");
break;
}
Console.WriteLine("按任意键继续");
Console.ReadKey(true);
}
}
}
}