2013-02-28 26 views
2

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

请注意,您发布的代码中的所有内容都是普通的C,而不是Objective-C。这没什么错,因为所有有效的C也是有效的Objective-C,但它不是通常编写Objective-C程序的方式。 – 2013-02-28 00:39:24

+0

@AndrewMadsen你可以标记我的帖子 – codegeek511 2013-02-28 03:58:19

回答

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

“什么?”,那么,将是你的默认回答。

+0

哦,很酷,谢谢。 – codegeek511 2013-02-28 00:40:22

+0

不客气。你对问题的想法是正确的。 – trudyscousin 2013-02-28 00:40:57

+0

我终于得到了如何提问,我注意到你说我的问题不是问题。 – codegeek511 2013-02-28 00:41:14

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

然后,比你的两个预期输入任何其他,将打印“我不明白”

+0

感谢您的帮助 – codegeek511 2013-02-28 00:42:46

相关问题