做while循环没有正确循环

时间:2015-09-07 14:52:21

标签: c++

在战斗游戏中练习,暂时保持基础,我试图让用户继续输入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
            }

    }

1 个答案:

答案 0 :(得分:3)

您正在

中返回totalMonsterHealth
if(battleChoice =='A' || battleChoice == 'a')

因此,当用户点击Aa时,它将退出循环