C#使用while循环实现英雄A与英雄B的对战过程
答案:1 悬赏:40 手机版
解决时间 2021-03-28 16:51
- 提问者网友:斑駁影
- 2021-03-28 03:18
C#使用while循环实现英雄A与英雄B的对战过程
最佳答案
- 五星知识达人网友:荒野風
- 2021-03-28 04:18
class Hero
{
public int ATN { get; set; }
public int DEF { get; set; }
public int HP { get; set; }
public string Name { get; set; }
public bool Attack(Hero target)
{
bool hasDead = false;
int damage = this.ATN - target.DEF;
target.HP -= damage;
Console.WriteLine("{0}向{1}发动攻击,造成{2}点伤害!", this.Name, target.Name, damage, target.HP);
if (target.HP <= 0)
{
Console.WriteLine("{0}已经死亡!", target.Name);
target.HP = 0;
hasDead = true;
}
Console.WriteLine("{0}生命值变为{1}", target.Name, target.HP);
return hasDead;
}
static void Main()
{
Hero A = new Hero() { Name = "卡特琳娜", ATN = 78, DEF = 30, HP = 200 },
B = new Hero() { HP = 300, Name = "盖伦", ATN = 100, DEF = 45 };
Console.WriteLine("---------------------
英雄联盟
---------------------");
while (A.HP >= 0 && B.HP >= 0)
{
if (B.Attack(A))
{
Console.WriteLine("{0}获取胜利", B.Name);
break;
}
if (A.Attack(B))
{
Console.WriteLine("{0}获取胜利", A.Name);
break;
}
}
Console.WriteLine("请按任意键继续。。。");
Console.ReadKey();
}
}
{
public int ATN { get; set; }
public int DEF { get; set; }
public int HP { get; set; }
public string Name { get; set; }
public bool Attack(Hero target)
{
bool hasDead = false;
int damage = this.ATN - target.DEF;
target.HP -= damage;
Console.WriteLine("{0}向{1}发动攻击,造成{2}点伤害!", this.Name, target.Name, damage, target.HP);
if (target.HP <= 0)
{
Console.WriteLine("{0}已经死亡!", target.Name);
target.HP = 0;
hasDead = true;
}
Console.WriteLine("{0}生命值变为{1}", target.Name, target.HP);
return hasDead;
}
static void Main()
{
Hero A = new Hero() { Name = "卡特琳娜", ATN = 78, DEF = 30, HP = 200 },
B = new Hero() { HP = 300, Name = "盖伦", ATN = 100, DEF = 45 };
Console.WriteLine("---------------------
英雄联盟
---------------------");
while (A.HP >= 0 && B.HP >= 0)
{
if (B.Attack(A))
{
Console.WriteLine("{0}获取胜利", B.Name);
break;
}
if (A.Attack(B))
{
Console.WriteLine("{0}获取胜利", A.Name);
break;
}
}
Console.WriteLine("请按任意键继续。。。");
Console.ReadKey();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯