Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        3年前关闭。
                                                                                            
                
        
我有问题要解决哪两个假设对手正在以RPG风格决斗。我被要求给他们HP,而每轮回合中,HP都对他们的对手所追求的目标失去了尊敬。我对此有基本的了解,但我的数字还在不断重复。在Visual Studios上工作15.提前致谢!

/*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);
}

最佳答案

您正在循环中声明新变量:

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);

07-27 13:33