帮助处理 C++ 中的对象

发布于 2024-10-01 11:13:52 字数 1249 浏览 2 评论 0原文

我的代码

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 时,没有任何反应并且代码不断循环。我做错了什么?

I have the code

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

which compiles fine, however, when I try to set "ogre.isalive" (at the very bottom of the code) to false, nothing happens and the code keeps looping. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞旧 2024-10-08 11:13:58

您在循环条件中有赋值 (ogre.isalive = true)。应该是:

while(ogre.isalive)
...

You have assignment (ogre.isalive = true) in the loop condition. It should be:

while(ogre.isalive)
...
羁客 2024-10-08 11:13:56

使用

                     vv
 while( ogre.isalive == true )

或只是

 while( ogre.isalive )

编辑:使用

                     v
 while( ogre.isalive = true )

只是在循环的每个步骤上分配 true ,这样你的 while 将永远不会停止。

Use

                     vv
 while( ogre.isalive == true )

Or just

 while( ogre.isalive )

EDIT: Using

                     v
 while( ogre.isalive = true )

just assigns true on each step of the loop, this way your while will never stop.

南冥有猫 2024-10-08 11:13:55

你的 while 条件是一个赋值,然后是对 ogre.isalive 的检查,这当然是正确的,因为你刚刚分配了它:

while(ogre.isalive = true)

你想要检查是否相等:

while(ogre.isalive == true)

或者更好,因为变量已经是一个布尔值:

while(ogre.isalive)

Your while condition is an assignment and then a check of ogre.isalive, which is of course true because you just assigned it:

while(ogre.isalive = true)

You want to check for equality:

while(ogre.isalive == true)

Or better yet, since the variable is already a boolean:

while(ogre.isalive)
护你周全 2024-10-08 11:13:54
ogre.isalive = true 

是一个作业! while 循环将始终运行,因为赋值的结果始终为 true。您需要 == 来测试相等性。更好的是,只需使用

while(ogre.isalive)
ogre.isalive = true 

is an assignment! The while loop will always run because the result of the assigment is always true. You need == to test for equality. Better yet, just use

while(ogre.isalive)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文