为什么"do while"循环不断退出,即使条件计算结果为 false?
Why "do while" loop keeps exiting, even though the condition evaluates to false?
当我运行代码时,它应该只有在所有龙没有生命值或所有船没有生命值之后才能获得胜利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循环吗?
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 在循环条件 i<sqrtN(预先计算)和 i*i<N 中,C++哪个更有效?
- C++ 中的"if"语句不会从左到右计算条件
- 当我的 if 条件计算结果为 false 时,我的 else 块将不会执行
- 如何使用条件计算 3D 网格中从一个点到另一个点的所有路径
- 计算另一个图像像素满足条件的像素值的平均值
- 在C++中,是否可以编写一个条件的lambda,即只是一个计算结果为真或假的条件
- getline() 在 while 循环条件中如何计算为真或假?
- 如果满足嵌套条件,则计算结果未显示结果C
- 为什么这个条件运算符的计算结果为 int?
- 递归计算满足条件的值数并返回该数字
- 如果语句计算结果为 false,即使条件为真
- 计算文本文件中行数的最佳条件是什么
- 使用条件运算符递归计算模板化值或函数时出现错误 C1202(堆栈溢出)
- "if"条件的计算结果始终为 "true"
- 如果在循环条件下使用,将多次计算 strlen
- 条件断点:此表达式具有副作用,不会计算
- C++嵌套的条件运算符计算顺序
- 用于计算条件为真的实例的代码