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

2 に答える 2

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

次に、予想される 2 つの入力以外の場合は、"I don't understand" と出力されます。

于 2013-02-28T00:38:18.967 に答える
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?");
}

「なに?」というのがデフォルトの答えです。

于 2013-02-28T00:38:37.217 に答える