C++ 当循环条件不工作时

C++ 当循环条件不工作时,c++,while-loop,text-based,C++,While Loop,Text Based,嘿,基本上,我希望玩家和狼群互相攻击,直到对方死亡。但是while循环是无限的,因此显然不满足条件。但是如果choice1==1//if语句在整个游戏中使用,允许用户在整个游戏中与选项交互,那么我看不出我的错误在哪里 while((Status.health != 0) && (Wolves.health != 0) ) { int playerAttack = Status.strength + hitPoints() + Rock.attac

嘿,基本上,我希望玩家和狼群互相攻击,直到对方死亡。但是while循环是无限的,因此显然不满足条件。但是如果choice1==1//if语句在整个游戏中使用,允许用户在整个游戏中与选项交互,那么我看不出我的错误在哪里

while((Status.health != 0) && (Wolves.health != 0) )
        {
        int playerAttack = Status.strength + hitPoints() +  Rock.attack;
        cout<< "This is the player attack" << playerAttack;
        Wolves.health = Wolves.health - playerAttack;
        cout << "This is the wolves health" << Wolves.health;
        if (Wolves.health <= 0)
        {
            cout << "\nThe wolves are dead\n ";
        }
        int wolfAttack = Wolves.attack + hitPoints();
        Status.health - wolfAttack;
        if(Status.health <= 0)
        {
            gameOver();
        }// print out of object health.
        }
有人能帮忙吗?

比较:

Wolves.health = Wolves.health - playerAttack;
vs


注意到任何差异吗?

您确定这是正确的:

Status.health - wolfAttack;
这实际上是一个无操作。也许你的意思是:

Status.health -= wolfAttack;

很可能这两个值永远都不是零。这听起来很有可能,因为你自己使用了我认为你得到的是负值,我建议使用

while((Status.health > 0) && (Wolves.health > 0) )

嗯,我认为健康状况不完全是0-因为你的状况只针对0 它应该大于0

whileStatus.health>0&&wolfs.health>0


编辑:还有在计算机中首先找到的缺失的=John Dibling,有些数字无法表示,例如0.1,因此如果计算1-0.1*10,其结果不等于零。你只检查了=0和==0条件。试试这个:


=0或运行状况是否会小于0?我从来没有说过如果运行状况小于0,如果这是你的意思。你试过调试你的代码吗?嗯,你的while条件只是检查以确保两个值都不是零。很可能你正在将生命值降低到0以下,因此while条件仍然为真。哦,我现在明白了。因为值小于零,所以它没有执行while条件。
while((Status.health > 0) && (Wolves.health > 0) )