gpt4 book ai didi

c++ - 帮助 C++ 中的对象

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:05 24 4
gpt4 key购买 nike

我有密码

class ogre
{
public:
int health;
bool isalive;
string name;
};

int fight()
{
cout << "You're now fighting an ogre!" << endl;

ogre ogre;
player player;
int ogredamage;
int playerdamage;

srand(time(NULL));

ogre.name = "Fernando Fleshcarver";
ogre.health = 100;
ogre.isalive = true;

player.health = 10000000;
player.isalive = true;

while(ogre.isalive = true)
{
ogredamage = rand() % 20;
cout << ogre.name << " deals " << ogredamage << " damage to you!" << endl;
player.health = player.health - ogredamage;
cout << "You have " << player.health << " health left." << endl << endl;

playerdamage = rand() % 20;
cout << "You deal " << playerdamage << " damage to " << ogre.name << endl;
ogre.health = ogre.health - playerdamage;
cout << ogre.name << " has " << ogre.health << " health left." << endl;

ogre.isalive = false;

}

return 0;
}

编译正常,但是,当我尝试将“ogre.isalive”(在代码的最底部)设置为 false 时,什么也没有发生,代码一直在循环。我做错了什么?

最佳答案

ogre.isalive = true 

是一个任务! while 循环将始终运行,因为分配的结果始终为真。您需要 == 来测试相等性。更好的是,只需使用

while(ogre.isalive)

关于c++ - 帮助 C++ 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4121855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com