简体   繁体   English

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

[英]Why does this do/while never end?

It just keeps looping. 它只是保持循环。 The numbers continue decreasing until the program is closed. 数字继续减少,直到该计划结束。 Am I misusing something? 我滥用了什么吗?

The playerHealth and orcHealth ints are 100. playerHealth和orcHealth ints是100。

randomNumber = ("%10d", 1 + (rand() % 100));

This was the way I seen the random number used on an srand() explanation page. 这是我在srand()解释页面上看到的随机数的方式。 If this is wrong, how should it be? 如果这是错的,应该怎么做?

Are there any other problems here? 这里还有其他问题吗?

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

playerHealth || orcHealth >= 0 playerHealth || orcHealth >= 0 does not mean "while playerhealth greater than zero OR orchealth greater than zero". playerHealth || orcHealth >= 0 并不意味着“而playerhealth大于零或者orchealth大于零”。 It means "while playerhealth, cast to boolean is true OR orchealth greater than zero". 这意味着“当玩家健康时,施放到布尔值是真或者OR orchealth大于零”。

This 这个

}while(playerHealth || orcHealth >= 0);

should probably be 应该是

}while(playerHealth > 0 && orcHealth > 0);

I think you want to exit the loop, if either one is 0 or less. 我想你想退出循环,如果其中一个是0或更少。

Also, change randomNumber = ("%10d", 1 + (rand() % 100)); 另外,更改randomNumber = ("%10d", 1 + (rand() % 100)); to randomNumber = 1 + rand() % 100; to randomNumber = 1 + rand() % 100; The comma operator just obfuscates the code. 逗号运算符只是模糊代码。

Your condition for the do...while statement will stop at some point, but only at some point. do...while语句的条件将在某个时刻停止,但仅在某个时刻停止。 This means, your condition will be met if either playerHealth is zero or orcHealth is smaller than zero. 这意味着,你的病情会如果任何满足playerHealth为零或orcHealth小于零。 What if the playerHealth gets below zero? 如果playerHealth低于零怎么办? This is very likely since you are always deducting a number from both character's healths. 这很可能是因为你总是从两个角色的健康状况中扣除一个数字。 A possibility of playerHealth becoming exactly zero is a very tiny one. playerHealth成为零的可能性非常小。 And when the playerHealth goes below zero, its possibility of becoming zero is still very unlikely, even due to integer overflow. playerHealth低于零时,即使由于整数溢出,其变为零的可能性仍然非常小。 So, if you want to "kill" the character when one of their health becomes zero or less, you better change that line with something like 所以,如果你想在他们的一个健康状态变为零或更少时“杀死”这个角色,你最好用这样的东西改变那一行

while ( playerHealth > 0 && orcHealth > 0 )

On a side note, the || 在旁注, || statement works if any one of the statements is true. 如果任何一个陈述为真,则语句有效。 In C++, for an integer a 0 value is false, all other values -including negative ones- are treated as true. 在C ++中,对于整数, 0值为false,所有其他值(包括负值)都被视为true。 Also, the || 另外, || checks from left to right and when it finds the first true statement, it stops searching. 从左到右检查,当找到第一个true语句时,它停止搜索。 In your case, it checks playerHealth , which is very likely nonzero. 在你的情况下,它会检查playerHealth ,这很可能是非零的。 When it sees this expression is true, it decides that the whole statement inside the parantheses is true and skips checking orcHealth >= 0 . 当它看到这个表达式为真时,它确定orcHealth >= 0中的整个语句为true并跳过检查orcHealth >= 0 This results in the infinite loop. 这导致无限循环。 You might want to look at the order of evaluation of conditionals in C++ , maybe something like this post . 你可能想看看C++中条件的评估顺序, 也许就像这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM