当用户键入错误的输入时,如何在文本游戏中创建默认答案?

时间:2013-02-28 00:35:37

标签: objective-c c

这是我的代码到目前为止,我正在使用目标c。它很小,但我想做一个默认的答案,例如,如果有人输入“我喜欢馅饼”它会说“我不明白”。

    printf("This is a text game! You will be shown what is going on");
    printf("\nand it is up to you to decide what to do.");

    printf("\n\nThere is a gem on the ground.");
    printf("\nWhat do you want to do");
    printf("\n>");

    char string[256];
    fgets(string, 255, stdin);

    if (strcmp(string, "pick up gem\n") == 0)
    {
        printf("Got Gem");
    }
    else if (strcmp(string, "kick gem\n") == 0){
        printf("Gem flew off the road.");
    }     

2 个答案:

答案 0 :(得分:2)

为什么不呢:

if (strcmp(string, "pick up gem\n") == 0){
    printf("Got Gem");
}
else if (strcmp(string, "kick gem\n") == 0){
    printf("Gem flew off the road.");
}   
else{
    printf("I don't understand.");
}

然后,对于除了两个预期输入之外的任何内容,它将打印"我不理解"

答案 1 :(得分:1)

你可以写:

if (strcmp(string, "pick up gem\n") == 0)
    {
        printf("Got Gem");
    }
    else if (strcmp(string, "kick gem\n") == 0){
        printf("Gem flew off the road.");
    }
else {
    printf("What?");
}

“什么?”然后,将是您的默认答案。