【问题标题】:End a loop if the user select a specific function如果用户选择特定功能,则结束循环
【发布时间】:2016-01-15 11:00:48
【问题描述】:

您好,我需要一点帮助来解决问题!

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

这是我的代码:

#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 中的一个参数,但对我没有任何作用。

【问题讨论】:

  • 顺便说一句,提前非常感谢!

标签: c function loops pointers do-while


【解决方案1】:

当用户选择 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);

}

【讨论】:

  • 请注意,第三种方法要求stdlib.h 包含在exit 函数中。
【解决方案2】:

如果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);

【讨论】:

    【解决方案3】:

    我希望你想逃离case 4 中的循环:

    尝试使用goto

        case 4 :
            Quit_Game();
            goto out;
            break;
        }
    
    } while (days_remaining>0 && current_hp>0);
    out:
    return 0;
    

    【讨论】:

    • 谢谢大家!你们都给出了很好的答案!
    • break; 永远不会被执行,因此应该被删除。并且使用gotos 被认为是不好的做法。
    【解决方案4】:

    (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 表示假,其他数字表示真,最好是 1。

    【讨论】:

    • 请注意,bool quit = false; 需要 stdbool.h,它在 C99 及更高版本中可用。 "不支持保留字,如@9​​87654329@ 和false" -- "reserved"?此外,它们中定义在stdbool.h 中。 "return 0; break;" -- break; 永远不会被执行,因此应该被删除。
    • 感谢您的反馈!我知道休息的情况,我无意中离开了。并感谢您对 C99 库的提示,不知道 true 和 false 能够与那些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2016-09-01
    • 2012-08-18
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多