做 while 循环不正确循环
do while loop not looping properly
练习对战游戏,暂时保持基础,尝试让用户继续输入'A'进行攻击,希望循环让用户继续攻击直到怪物血量是0或低于0。问题是它攻击一次,它减少了生命值并显示了它剩余的生命值但是,它不允许用户再次攻击。这是我目前坚持的作品。
if (level ==1 )
{
do
{
cout<<"\n\n";
cout<<"Level "<<level<<" Defeat the guard\n\n";
cout<<"Guard Stats\n";
cout<<"Power: "<<monster.displaystrength()<<endl;
cout<<"Defense: "<<monster.displaydefense()<<endl;
cout<<"Health: "<<monster.displayhealth()<<"\n\n"<<endl;
cout<<"Enter A) Attack\n";
cout<<"Enter D) Defend\n";
cout<<"Enter S) Special\n";
cout<<"Enter N) Do Nothing\n\n";
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<<"\n\n"<<endl;
return totalMonsterHealth;
}
}while (totalMonsterHealth!=0);
if(battleChoice == 'D' || battleChoice == 'd')
{
cout<<"Defending\n"; //prototype
}
if(battleChoice == 'S' || battleChoice == 's')
{
cout<<"Using special attack\n"; //prototype
}
if(battleChoice == 'N' || battleChoice == 'n')
{
cout<<"Do nothing, Loop again.\n"; //prototype
}
}
您将在 totalMonsterHealth
返回
if(battleChoice =='A' || battleChoice == 'a')
所以当用户点击 A
或 a
它将退出循环
练习对战游戏,暂时保持基础,尝试让用户继续输入'A'进行攻击,希望循环让用户继续攻击直到怪物血量是0或低于0。问题是它攻击一次,它减少了生命值并显示了它剩余的生命值但是,它不允许用户再次攻击。这是我目前坚持的作品。
if (level ==1 )
{
do
{
cout<<"\n\n";
cout<<"Level "<<level<<" Defeat the guard\n\n";
cout<<"Guard Stats\n";
cout<<"Power: "<<monster.displaystrength()<<endl;
cout<<"Defense: "<<monster.displaydefense()<<endl;
cout<<"Health: "<<monster.displayhealth()<<"\n\n"<<endl;
cout<<"Enter A) Attack\n";
cout<<"Enter D) Defend\n";
cout<<"Enter S) Special\n";
cout<<"Enter N) Do Nothing\n\n";
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<<"\n\n"<<endl;
return totalMonsterHealth;
}
}while (totalMonsterHealth!=0);
if(battleChoice == 'D' || battleChoice == 'd')
{
cout<<"Defending\n"; //prototype
}
if(battleChoice == 'S' || battleChoice == 's')
{
cout<<"Using special attack\n"; //prototype
}
if(battleChoice == 'N' || battleChoice == 'n')
{
cout<<"Do nothing, Loop again.\n"; //prototype
}
}
您将在 totalMonsterHealth
if(battleChoice =='A' || battleChoice == 'a')
所以当用户点击 A
或 a
它将退出循环