과제 작업을 수행하는 동안 솔루션 검토
다음 코드는 앞 단원의 과제에 대한 가능한 한 가지 솔루션입니다.
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!
성공하면 축하합니다! 다음 과제를 계속 진행하세요. 문제가 있는 경우 시간을 내어 솔루션을 검토하고 작동 방식을 이해해 보세요. 계속하기 전에 이전 단원을 검토한 다음 이 챌린지 작업을 다시 시도할 수 있습니다.