ตรวจสอบโซลูชันเพื่อทําและในขณะที่กิจกรรมการทดสอบ

เสร็จสมบูรณ์เมื่อ

โค้ดต่อไปนี้เป็นหนึ่งในวิธีแก้ไขปัญหาที่เป็นไปได้สําหรับการทดสอบจากหน่วยก่อนหน้า

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!

ถ้าคุณประสบความสําเร็จ ขอแสดงความยินดี! ดําเนินการต่อไปยังการทดสอบถัดไป ถ้าคุณมีปัญหา ใช้เวลาในการตรวจสอบโซลูชัน และพยายามทําความเข้าใจวิธีการทํางาน คุณอาจต้องการตรวจสอบหน่วยก่อนหน้านี้ แล้วลองทํากิจกรรมการทดสอบนี้อีกครั้งก่อนดําเนินการต่อ