"否"选项在我的Do While循环中无法正常工作

时间:2018-05-12 23:13:32

标签: c# loops while-loop

所以我现在正在玩一个类似RPG的游戏,在经过一次攻击和反击之后,会出现一个对话框,询问你是否要离开,但问题是,"是"和不#34;两者都会导致它离开循环,通过点击" No"如果您的HP和敌人的HP高于0,它应该保持在循环中。我认为它工作正常,直到我稍后在程序中添加代码但我可能是错的:

DialogResult dialogResult;

do
{
    count++;
    if (enemyHP > 0)
    {
        Random rand = new Random();
        int chance = rand.Next(1, 101);

        MessageBox.Show("You see another enemy...);
        MessageBox.Show("Its HP is 10...2/5");
        MessageBox.Show("His current HP is " + enemyHP + ");
        if (chance <= 5)
        {
            MessageBox.Show("Miss");
        }
        else
        {
            MessageBox.Show("You dealt 5 damage to it.);
            enemyHP -= 5;
        }

        if (enemyHP > 0)
            MessageBox.Show("He also dealt 5 damage to you.);
        playerHP -= 5;
    }

    if (enemyHP <= 0)
    {
        MessageBox.Show("Enemy has been defeated!");
        dialogResult = (0);
    }
    else
     if (playerHP > 0)
    {
        dialogResult = MessageBox.Show("Do you want to leave?", "Message box", MessageBoxButtons.YesNo);
    }
} while (DialogResult == DialogResult.No && enemyHP > 0); 

0 个答案:

没有答案