2016-06-11 111 views
1

我有問題的工作,其中2假設oppenents決鬥RPG風格。我被要求給他們惠普,HP的每一回合都被他們的競爭對手所迷惑。我對它有基本的瞭解,但我的數字不斷重複。在Visual Studio上工作15.提前致謝!基本雖然循環c#

/*Fighter 1 is Charlie The Unicorn 
*Fighter 2 is Nyan Cat 
*Fighter 1 has 100 HP 
*Fighter 2 has 150 HP 
*Fighter 1 can hit as hard as 15HP each turn 
*Fighter 2 can hit as hard as 10HP each turn 
*/ 

//Who wins the fight 

int fighterOneHealth = 100; 
int fighterTwoHealth = 150; 
Random randomizer = new Random(); 
while (fighterOneHealth > 0 || fighterTwoHealth > 0) 
{  
    int fOneRandomHit; 
    int fTwoRandomHit; 
    fOneRandomHit = randomizer.Next(0, 15); 
    fTwoRandomHit = randomizer.Next(0, 10); 
    int fightOneHpLoss = fighterOneHealth - fTwoRandomHit; 
    int fightTwoHpLoss = fighterTwoHealth - fOneRandomHit; 
    Console.WriteLine("After attack: Charlie the Unicorn HP: {0}, Nyan Cat HP: {1}", fightOneHpLoss, fightTwoHpLoss); 
} 
+0

你永遠不會修改你在條件語句'fighterOneHealth> 0 ||中檢查的值。 fighterTwoHealth> 0' – jdphenix

+0

此外,變量名稱「fightOneHpLoss」和「fightTwoHpLoss」似乎讓我感到困惑,因爲它們是應用隨機命中後留下的HP。也許你的意思是'fighterOneHealth - = fTwoRandomHit'等? – jdphenix

回答

6

你聲明環內新變量:

int fightOneHpLoss = fighterOneHealth - fTwoRandomHit; 
int fightTwoHpLoss = fighterTwoHealth - fOneRandomHit; 

...但你永遠不會修改這意味着可以代表球員當前健康的變量。你根本不需要這些額外的變量 - 你只需要減少健康狀況:

fighterOneHealth -= fTwoRandomHit; 
fighterTwoHealth -= fOneRandomHit; 
Console.WriteLine("After attack: Charlie the Unicorn HP: {0}, Nyan Cat HP: {1}", 
    fighterOneHealth, fighterTwoHealth); 
+1

非常有幫助。謝謝喬恩。猜猜我只是在想它。 – Charlie