为什么这样做/永远不会结束?

Hyr*_*x77 2 c++ infinite-loop do-while

它只是保持循环.数字继续减少,直到该计划结束.我滥用了什么吗?

playerHealth和orcHealth ints是100.

randomNumber = ("%10d", 1 + (rand() % 100));
Run Code Online (Sandbox Code Playgroud)

这是我在srand()解释页面上看到的随机数的方式.如果这是错的,应该怎么做?

这里还有其他问题吗?

    switch(charDecision)
{
case 1:
    cout << "FIGHT" << endl;
    do{
        randomNumber = ("%10d", 1 + (rand() % 100));
        if(randomNumber >= 50){
            orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
        cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
        }
        else
        {
            playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
            cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
        }
    }while(playerHealth || orcHealth >= 0);
    break;

default:
    break;
}
Run Code Online (Sandbox Code Playgroud)

MSa*_*ers 18

playerHealth || orcHealth >= 0没有意思"而playerhealth大于零或者orchealth大于零".这意味着"当玩家健康时,施放到布尔值是真或者OR orchealth大于零".

  • 即玩家健康需要正好为0来评估为假,好抓 (5认同)

Hen*_*rik 9

这个

}while(playerHealth || orcHealth >= 0);
Run Code Online (Sandbox Code Playgroud)

应该是

}while(playerHealth > 0 && orcHealth > 0);
Run Code Online (Sandbox Code Playgroud)

我想你要退出循环,如果其中一个是0或更少.

此外,更改randomNumber = ("%10d", 1 + (rand() % 100));randomNumber = 1 + rand() % 100; 逗号运算符只是混淆代码.

  • @fiscblog你认为战斗应该继续,直到两人都死了? (7认同)