Delineare la soluzione per l'attività di sfida relativa a do e while
Il codice seguente costituisce una possibile soluzione alla sfida presentata nell'unità precedente.
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!");
Questo codice è semplicemente "una possibile soluzione" perché esistono molti modi diversi per eseguire la logica di attacco.
In ogni modo, l'output dovrebbe essere simile a quello dell'esempio seguente:
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!
Se hai avuto successo, congratulazioni! Passare al prossimo test. In caso di problemi, esaminare la soluzione e provare a capire come funziona. È possibile rivedere le unità precedenti e ripetere l'attività di verifica prima di continuare.