Do while循环没有正确循环
do while loop not looping properly
在一款战斗游戏中进行练习,目前保持它的基础,我试图让用户继续输入"a"进行攻击,我希望循环让用户继续攻击,直到怪物的生命值为0或低于0。问题是,它攻击一次,它会减少生命值并显示剩余生命值,但它不允许用户再次攻击。这是我目前被卡住的部分。
if (level ==1 )
{
do
{
cout<<"nn";
cout<<"Level "<<level<<" Defeat the guardnn";
cout<<"Guard Statsn";
cout<<"Power: "<<monster.displaystrength()<<endl;
cout<<"Defense: "<<monster.displaydefense()<<endl;
cout<<"Health: "<<monster.displayhealth()<<"nn"<<endl;
cout<<"Enter A) Attackn";
cout<<"Enter D) Defendn";
cout<<"Enter S) Specialn";
cout<<"Enter N) Do Nothingnn";
cin>>battleChoice;
if(battleChoice =='A' || battleChoice == 'a')
{
srand(time(NULL));
dps = rand()%10+1;
cout<<"Damage done: " << dps << endl;
monsterHealth = monster.displayhealth();
totalMonsterHealth = monsterHealth - dps;
cout<<"Monster health remaining: "<<totalMonsterHealth<<"nn"<<endl;
return totalMonsterHealth;
}
}while (totalMonsterHealth!=0);
if(battleChoice == 'D' || battleChoice == 'd')
{
cout<<"Defendingn"; //prototype
}
if(battleChoice == 'S' || battleChoice == 's')
{
cout<<"Using special attackn"; //prototype
}
if(battleChoice == 'N' || battleChoice == 'n')
{
cout<<"Do nothing, Loop again.n"; //prototype
}
}
您正在返回totalMonsterHealth
if(battleChoice =='A' || battleChoice == 'a')
当用户点击A
或a
时它将退出循环
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么在 while 循环中返回表达式不起作用
- 如何使用 do while 循环确定最高值和最低值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- OpenCV QT,显示视频的帧(不使用while循环)
- 输入循环 while 函数
- c++中的循环(while)
- 为什么这段代码没有循环(while)