objective-c - 当用户输入错误的输入时,如何在文字游戏中做出默认答案?

标签 objective-c c

这是我到目前为止的代码,我正在使用 Objective 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.");
    }     

最佳答案

为什么不只是:

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.");
}

然后,对于除两个预期输入之外的任何内容,它将打印“我不明白”

关于objective-c - 当用户输入错误的输入时,如何在文字游戏中做出默认答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125366/

相关文章:

ios - 调整 UITextView 的大小在新行的前 2 个字符上无法正常工作

ios - UIViewController 通过 xib 加载另一个 View Controller ,试图访问默认 View 导致崩溃

ios - 为什么这里需要类型转换?

c - 之后使用 rand() 和 srand(time(NULL)) 不会生成新的随机数

ios - 条件语句中的 Objective-C 转换为自定义类

objective-c - 在 mac os x app cocoa 中生成图像文件的问题

CC3200 SPI 从机接收不良

c - 对结构体上的字符串进行选择排序

c - tic-tac-toe 使用 opencv 但我可以在识别游戏板时停止

c - 在 C 中的变量上使用 'printf'