查看实现 do 和 while 挑战活动的解决方案

已完成

以下代码是上一单元中所述挑战的一种可能的解决方案。

int hero = 10;
int monster = 10;

Random dice = new Random();

do
{
    int roll = dice.Next(1, 11);
    monster -= roll;
    Console.WriteLine($"Monster was damaged and lost {roll} health and now has {monster} health.");

    if (monster <= 0) continue;

    roll = dice.Next(1, 11);
    hero -= roll;
    Console.WriteLine($"Hero was damaged and lost {roll} health and now has {hero} health.");

} while (hero > 0 && monster > 0);

Console.WriteLine(hero > monster ? "Hero wins!" : "Monster wins!");

此代码仅仅是“一种可能的解决方案”,因为执行攻击逻辑有很多种不同的方法

但无论如何,输出都应类似于以下示例:

Monster was damaged and lost 1 health and now has 9 health.
Hero was damaged and lost 2 health and now has 8 health.
Monster was damaged and lost 1 health and now has 8 health.
Hero was damaged and lost 4 health and now has 4 health.
Monster was damaged and lost 7 health and now has 1 health.
Hero was damaged and lost 6 health and now has -2 health.
Monster wins!

如果你成功了,恭喜! 继续下一个挑战。 如果遇到问题,请花时间查看解决方案,并尝试理解其工作原理。 你可能想要查看以前的单元,然后在继续之前重试此挑战活动。