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

时间:2019-10-08 23:39:36

标签: c++

Image of error它询问了两次问题,只需要询问一次,但是循环运行良好且与程序的其他部分不一致。谢谢您的任何想法或建议,大学课堂!

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

创建以下全局变量:healthtotalTreasure

游戏应如下运行:

玩家被告知一个故事,促使他们在冒险中寻求宝藏。询问玩家是否想冒险。如果玩家回答“是”,则会随机生成敌人攻击的数字,自己的格挡和一定数量的宝物。如果玩家的格挡高于敌人的攻击,则说明他们已成功格挡并获得了宝藏。但是,如果攻击强度更高,则从他们的健康中减去该数字,他们将无法获得宝藏。然后,告知玩家其健康状况和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");
}

没有错误消息。

3 个答案:

答案 0 :(得分:1)

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

我认为,一开始,您应该修复条件的组合。如果第一个错误,则第二个永远不可能为真。

由于您声明“永远无法获得宝藏”,并且获得的收益永远无法执行,因此至少可以解决您的问题之一。

我怀疑您想要的是这样的东西

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

在所有情况下均具有适当的输出。

答案 1 :(得分:1)

问题出在两次调用askYesNo()函数上。如果要在askYesNo()条件内调用while(),则无需在askYesNo()内再次调用if()。因此,基本上是重复一遍。

答案 2 :(得分:0)

我认为您应该像这样使用简单的while

while (playerHealth > 0)
{
    if (askYesNo("Do you want to keep adventuring? (y/n)") == "y")
    {
        // do the yes part
    }
    else
    {
        // do no part
        return; // this will break your while
    }
}