gpt4 book ai didi

c++ - 无法弄清楚为什么它不会停止循环

转载 作者:行者123 更新时间:2023-11-28 01:17:13 26 4
gpt4 key购买 nike

Image of error它问了两次问题,只需要问一次,但循环运行良好并且与程序的其他部分一致。谢谢你的任何想法或建议,顺便说一句,大学类(class)!

在一个新程序中创建以下函数:StoryAskYesNo;一个名为 questionRollDie 的字符串参数;一个名为 sides 的 int 参数,默认值为 6,EndingAdventure,显然是 main

创建以下全局变量:healthtotalTreasure

游戏应该运行如下:

玩家被告知一个故事,促使他们去冒险寻找宝藏。询问玩家是否愿意去冒险。如果玩家说是,则随机生成敌人攻击的数字、他们自己的方 block 和一定数量的宝藏。如果玩家的格挡高于敌人的攻击力,则他们成功格挡并获得宝物。但是,如果攻击力更高,则该数字会从他们的生命值中减去,并且他们不会获得宝藏。然后玩家会被告知他们的健康状况和 totalTreasure 的数量,并询问他们是否愿意再次冒险。只要他们的生命值大于零,他们就可以继续冒险,此时运行结局并告诉他们他们已经死了如果他们在他们的生命值为零之前退出,他们会被告知他们有多少健康以及如何他们最终得到了很多宝物。

// Global variables
int playerHealth = 15;
int playerTreasure = 0;

// Function Prototypes
void ending();
void story();
void adventure();
int rollDie(int sides = 6);

int main() { //My so called storyboard lol
    srand(time(0));
    story();
    adventure();
    ending();
    return 0;
}

//Functions
void ending() {
    if (playerHealth <= 0) {
        cout << endl;
        cout << "Your out of health! :(\n";
        cout << "The treasure you end up with is " << playerTreasure << " pieces!\n";
        cout <<"Thanks for adventuring!\n";
    } else { // if (askYesNo("Do you want to keep adventuring? (y/n)") == "n")
        cout << "You retired from adventuring!\n";
        cout << "You retired with " << playerTreasure << " treasure pieces!\n";
    }
    return;
}

void story() {
cout << "You wake up in a forrest, by yourself\n";
cout << "You only have " << playerHealth << " health points left.\n";
cout << endl; 
}

string askYesNo(string givenQuestion) {
    string input;
    do {
        cout << givenQuestion << endl;
        cin >> input;
    } while (input != "y" && input != "n");
    return input;
}

int rollDie(int sides) {
    return rand() % sides + 1;
}

void adventure() {
    do {
        if (askYesNo("Do you want to keep adventuring? (y/n)") == "y")
        {
            int enemyAttack = rollDie(100);
            int playerBlock = rollDie(100);
            if (enemyAttack > playerBlock)
            {
                playerHealth -= 3;
                cout << "The enemy attack points is " << enemyAttack << "\n";
          cout << "Your player block is " << playerBlock << "\n";
                cout << "Your current health is " << playerHealth << " .\n";
                cout << "You did not gain any treasure\n"; 
            } else if (playerBlock >= enemyAttack)
            {
                playerTreasure += rollDie(6);
                cout << "The enemy attack points is " << enemyAttack << "\n";
                cout << "Your player block is " << playerBlock << "\n";
                cout << "You have blocked!\n";
                cout << "You gain treasure! your current treasure is " << playerTreasure << " \n"; 
            }
        } else {
            cout << "Your current health is " << playerHealth << " .\n";
            cout << "Your current amout of treasure is " << playerTreasure << " \n";
            return;
        }
    } while(playerHealth > 0 && askYesNo("Do you want to keep adventuring? (y/n)") == "y");
}

没有错误信息。

最佳答案

if (enemyAttack >= playerBlock)
else if (playerBlock < enemyAttack)

我认为,首先,您应该确定这些条件的组合。如果第一个为假,则第二个永远不会为真。

既然你说“宝藏是永远得不到的”,而获得的是永远无法执行的代码,那至少可以解决你的一个问题。

我怀疑你想要的是这样的:

if attack > block:
lose health
else if attack < health:
gain treasure
else:
do neither

当然在所有情况下都有合适的输出。

关于c++ - 无法弄清楚为什么它不会停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com