Files
Csharp/C#入门实践教程/随机数相关练习题/Program.cs
T
2025-09-30 16:49:43 +08:00

35 lines
891 B
C#

Random r = new Random();
int bossHealth = 20;
int bossDefense = 10;
int round = 1;
try
{
Console.WriteLine("游戏开始");
while (true)
{
int attack = r.Next(8, 13);
Console.WriteLine("当前第{0}回合",round);
if (attack >= bossDefense)
{
bossHealth -= attack - bossDefense;
Console.WriteLine("你攻击了BOSS,造成了" + attack + "点伤害\n" + "BOSS剩余血量:" + bossHealth);
}else
{
Console.WriteLine("你的攻击被BOSS防御住了,造成了0点伤害");
}
round++;
if (bossHealth <= 0)
{
Console.WriteLine("你打赢了BOSS,共使用了{0}回合",round-1);
break;
}
}
}catch (Exception ex)
{
Console.WriteLine("错误代码: " + ex.Message);
}
finally
{
Console.WriteLine("游戏结束");
}