如果用户 select 特定函数结束循环

End a loop if the user select a specific function

嗨,我需要一点帮助来解决问题![​​=12=]

我有一个包含循环的主 class。 在这个循环中,用户可以调用不同的函数来做不同的事情,但我希望如果用户 select 某个函数它会显示一条消息并且循环结束。

这是我的代码:

#include <stdio.h>

void rest_at_inn();
void train();
void battle_demon_lord();
void Quit_Game();

int main() {

    //Declare variable and pointer
    float days_remaining = 8;
    int current_hp = 10;
    int hp_remaining = 10;
    int MAX = 10;
    int experience = 0;
    int selection;

    //Loops a menu that asks the user to make a selectio and repet it until either days or HP are less or equal to zero
    do {
        printf("Days remaining: %f, HP: %d, EXP: %d\n\n", days_remaining, hp_remaining, experience);
        printf("1 - Rest at Inn\n");
        printf("2 - Train\n");
        printf("3 - Fight the Demon Lord\n");
        printf("4 - Quit Game\n\n");
        printf("Select: \n");
        scanf("%d", &selection);
        printf("\n");

        //Execute the selection
        switch (selection) {
            case 1:
                rest_at_inn(&days_remaining, &hp_remaining, &MAX);
                break;
            case 2:
                train(&days_remaining, &hp_remaining, &experience);
                break;
            case 3:
                battle_demon_lord(&current_hp);
                break;
            case 4:
                Quit_Game();
                break;
        }

    } while (days_remaining > 0 && current_hp > 0);

    return 0;

}

//This function reduces the days by 1 and resets the HP to 10

void rest_at_inn(float *days_remaining, int *hp_remaining, int *MAX) {

    *hp_remaining = 10;
    *days_remaining -= 1;
    if (*days_remaining > 0)
        printf("You rested up the at the inn\n\n");
    else /*(*days_remaining<=0)*/
        printf("Game Over");

}

//This function reduces the days by 0.5 and the HP by 2 and adds 10 to the experience

void train(float *days_remaining, int *hp_remaining, int *experience) {

    *experience += 10;
    *hp_remaining = *hp_remaining - 2;
    *days_remaining = *days_remaining - 0.5;
    printf("You did some training!\n\n");

}

//This function sets the HP to 0 and prints a string

void battle_demon_lord(int *current_hp) {

    *current_hp = 0;
    printf("He's too strong!\n\n");
    printf("Game Over!");
}

// This function prints a string

void Quit_Game() {

    printf("Good bye!\n\n");

}

我认为它应该是 do_while 中的一个参数,但对我没有任何作用。

如果 switch 是循环的最后一条语句,请在 do / while 循环的 do 之前添加一个 mustQuit 标志变量,并将其归零。当最终用户选择退出选项时,将 mustQuit 设置为 1。将 !mustQuit 条件添加到循环中。

int mustQuit = 0;
do {
    ... // some statements here
    switch (selection) {
    ...
    case 4 :
        mustQuit = 1;
        Quit_Game();
        break;
    }
} while (!mustQuit && days_remaining > 0 && current_hp > 0);

如果在 switch 之后有更多语句,中间带有条件 break 的替代方案可能更优化:

do {
    int mustQuit = 0;
    ... // some statements here
    switch (selection) {
    ...
    case 4 :
        mustQuit = 1;
        Quit_Game();
        break; // this breaks the switch
    }
    if (mustQuit) {
        break; // this breaks the loop
    }
    ... // More statements here
} while (days_remaining > 0 && current_hp > 0);

我想你想跳出 case 4 中的循环:

尝试使用goto

    case 4 :
        Quit_Game();
        goto out;
        break;
    }

} while (days_remaining>0 && current_hp>0);
out:
return 0;

您需要为用户选择 4 添加退出条件。

你可以这样做:

} while (days_remaining>0 && current_hp>0 && selection != 4);

或者这样:

case 4 :
    Quit_Game();
    return 0;

或者这样:

void Quit_Game()  {

    printf ("Good bye!\n\n");
    exit(0);

}

(1) 如果你想跳出 do-while 循环,你可以添加一个布尔变量,这样当它进入最后一个选项时它会是 false,像这样:

bool quit = false;

(...)

case 4 :
    quit = true;
    printf("Good bye!\n\n");
    break;

并在 do-while 的条件下添加值:

while (days_remaining>0 && current_hp>0 && !quit);

因此,它将跳出 do-while 循环并继续执行代码。

(2) 如果你想完成程序,添加一个 return 0 并忘记布尔值,像这样:

case 4 :
    printf("Good bye!\n\n");
    return 0;

记住:如果您在基于 C 的代码上执行此操作,它将不支持 truefalse 等保留字。您必须使用 0 表示 false,其他数字表示 true,最好是 1。