Do while循环没有正确循环

do while loop not looping properly

本文关键字:循环 while Do      更新时间:2023-10-16

在一款战斗游戏中进行练习,目前保持它的基础,我试图让用户继续输入"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')

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