在戰鬥遊戲上練習,現在保持基本狀態,我試圖讓用戶繼續輸入「A」進行攻擊,並且我希望循環讓用戶繼續攻擊,直到怪物的生命值是0或者低於0.問題是,它只會攻擊一次,它會降低生命值並顯示其剩餘的生命值,但不允許用戶再次攻擊。這是我目前堅持的作品。做while循環不正常循環
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
}
}
'return'從函數返回返回
totalMonsterHealth
。 – alain我猜測它的運行直到'return totalMonsterHealth'? ;-)該行終止函數,因此也是循環。 –