为什么"do while"循环不断退出,即使条件计算结果为 false?

Why "do while" loop keeps exiting, even though the condition evaluates to false?

本文关键字:计算 条件 结果 false while do 循环 退出 为什么      更新时间:2023-10-16

当我运行代码时,它应该只有在所有龙没有生命值或所有船没有生命值之后才能获得胜利condition = true
但当它运行时,do-while循环似乎在第一次迭代后退出,即使条件设置为只在win条件为true时离开
我似乎不明白为什么它不会继续运行,我甚至在声明变量时分配了winCondition = false
如有任何帮助,我们将不胜感激。

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
struct ship  //Creates type ship
{
float shipHealth;
int numOfPeople;
bool capturedDragon;
};
struct dragon  //Creates type dragon
{
string riderName;
string dragonName;
float dragonHealth;
bool isCaptured = false;
};
int main()
{
srand(time(NULL));
const int MAX_SHIPS = 7;
const int MAX_RIDERS = 5;
int dragonHit;
int dragonDamage;
int dragonRemoveMenNumber;
int shipDamage;
int shipHit;
bool winCondition = false;
ship hunterShips[MAX_SHIPS];  //creates array of ships
dragon dragonRiders[MAX_RIDERS];  //creates array of dragon/riders.
for (int i = 0; i <= 4; i++)
{
cout << "Rider Name: " << endl;
cin >> dragonRiders[i].riderName;
cout << "Dragon Name: " << endl;
cin >> dragonRiders[i].dragonName;
dragonRiders[i].dragonHealth = rand() % (20 - 15 + 1) + 15;
}
for (int i = 0; i <= 6; i++)
{
hunterShips[i].shipHealth = rand() % (40 - 30 + 1) + 30;
hunterShips[i].numOfPeople = rand() % (15 - 10 + 1) + 10;
}

do
{
for (int i = 0; i <= 4; i++)  //Dragons turn
{
if(dragonRiders[i].dragonHealth > 0 && dragonRiders[i].isCaptured == false)  //Dragon is alive
{
dragonHit = rand() % 10 + 1;
if (dragonHit <= 7)  //Dragon hits target
{
if(hunterShips[i].shipHealth > 0 || hunterShips[i].numOfPeople > 0)
{
dragonDamage = rand() % (10 - 5 + 1) + 5; //Amount of Damage done
cout << dragonRiders[i].dragonName << " hit and dealt " << dragonDamage << " damage. " << endl;
if (dragonHit <= 3)  //Dragon takes men out
{ 
dragonRemoveMenNumber = rand() % (3 - 2 + 1) + 2;
cout << dragonRiders[i].dragonName << "Took out " << dragonRemoveMenNumber << " men" << endl;
hunterShips[MAX_SHIPS].numOfPeople = hunterShips[MAX_SHIPS].numOfPeople - dragonRemoveMenNumber;  //Ships lose people
} 
hunterShips[MAX_SHIPS].shipHealth = hunterShips[MAX_SHIPS].shipHealth - dragonDamage;
}
else
{
cout << "All ships are destroyed, dragons win!" << endl;
winCondition = true;
}
}
else //Dragon misses target
cout << dragonRiders[i].dragonName << " missed" << endl;
}
else  //Dragon is dead
cout << dragonRiders[i].dragonName << " is dead or captured" << endl;  
}


for (int i = 0; i <= 6; i++) //Ships turn
{
if(hunterShips[i].shipHealth > 0 || hunterShips[i].numOfPeople > 0)  //ship is afloat
{
shipHit = rand() % 10 + 1;
if (shipHit >= 7)  //40% chance to hit 
{
if(dragonRiders[i].dragonHealth > 0 && dragonRiders[i].isCaptured == false)
{
shipDamage = rand() % (5 - 4 + 1) + 4; //Damage done
cout << "Ship dealt " << shipDamage << " damage" << endl;
dragonRiders[MAX_RIDERS].dragonHealth = dragonRiders[MAX_RIDERS].dragonHealth - shipDamage;
}
else
{
cout << "All dragons are dead, ships win!" << endl;
winCondition = true;
}
}
else
cout << "Ship missed. " << endl;
}
else
cout << "Ship is sunk or out of men" << endl; //ship is sunk
}
} while (winCondition == true);
return 0;
}

您的代码是

bool winCondition = false;
/* ... */
do {/* ... */}
while (winCondition == true);

并且您想知道"即使winCondition=false,循环仍保持退出"。您还解释了"条件设置为只有在获胜条件成立时才离开"。

我的结论是,您误解了do-while循环的语义为"循环直到满足条件"。相反,它是"满足条件时循环"。这意味着当winCondition评估为false时退出是预期行为。

您正在初始化winCondition = false。do..while循环将运行一次迭代,并评估迭代条件winCondition == true不满足,因此退出。

此外,如果do..while循环继续,则其中没有将winCondition设置为false的语句,因此该循环永远不会退出。

你的意思是用winCondition == false关闭do..while循环吗?