无论输入是什么,为什么两个if语句都被执行?

时间:2014-10-07 11:09:28

标签: c

在我选择你自己的冒险中,A和B都是导致打开输入功能的输入。无论哪个都被放下,它们都会显示出来。

#include <stdio.h>

int main()
{
    char firstname[15];
    char class;
    char swordch0c1;
    char enter;

    printf("Hello there! Could I have your first name?\n");
    scanf("%s",firstname);
    printf("\n---------------------The Legend of %s---------------------",firstname);
    printf("\nPress Enter to continue.");
    enter=getch();
    if(enter=='\n');

    printf("\n\n\nYou are %s, a: \nA.Swordsman\nB.Assassin\nC.Archer\nD.Mage\n",firstname);
    scanf("%c", &class);

    /*swordsman story starts here*/
    if(class=="A");
    {
    printf("\n\nThere you stand, at your boring everyday post.\nWhen you joined the army, you thought                it would be more exciting than this.\nJust then, you see your general walking towards you.");
    printf("\n\nYou quickly improve your posture. \"Soldier, I have an opurtunity for     you\"\nA.\"Really? What is it?\"\nB.\"I'm not interested\"\n");
    scanf("%d",swordch0c1);
    if(swordch0c1=="b");
        {
            printf("\"But... I didn't even tell you what it was. Okay, suit yourself\" You are DOOMED      to a life of boredom.\n\n\n\n\n");
        }
    if(swordch0c1=="a");
        {
            printf("\n\n\n\"Well, you see, there's this dragon. He's been causing big problems.     \nHe's destroyed villages, harrassed the priests on the mountain,\n");
            printf("and even attacked a couple cities. His name is Sorrith, and dozens of knights     have already tried to kill him, none of them being successful.\"");
            printf("\nA.\"Say no more, I'll do it.\"\nB.\"Dragon? No way! You better find someone     else.\nC.\"Keep talking...\"");
        }
    }

    return 0;
}

如果我不具体/清楚,我道歉。 我正在使用Windows BTW。

4 个答案:

答案 0 :(得分:2)

删除每个if后的所有分号,并在所有'中使用"代替if(...)。此外,

scanf("%d",swordch0c1);

错误,因为swordch0c1不是int。使用%c代替%d,因为它是您正在扫描的char

答案 1 :(得分:2)

你犯了一些错误,但这应该有效!

#include <stdio.h>

int main() {

    char firstname[15], character, swordch0c1, enter;

    printf("Hello there! Could I have your first name?\n>");
    scanf("%s", &firstname);

    printf("\n---------------------The Legend of %s---------------------", firstname);
    printf("\nPress Enter to continue.");
    enter=getch();

    printf("\n\n\nYou are %s, a: \nA.Swordsman\nB.Assassin\nC.Archer\nD.Mage\n>", firstname);
    scanf(" %c", &character);

    /*swordsman story starts here*/
    if (character == 'A') {
        printf("\n\nThere you stand, at your boring everyday post.\nWhen you joined the army, you thought it would be more exciting than this.\nJust then, you see your general walking towards you.");
        printf("\n\nYou quickly improve your posture. \"Soldier, I have an opurtunity for     you\"\nA.\"Really? What is it?\"\nB.\"I'm not interested\"\n>");
        scanf(" %c", &swordch0c1);

        if (swordch0c1 == 'b')
            printf("\"But... I didn't even tell you what it was. Okay, suit yourself\" You are DOOMED      to a life of boredom.\n\n\n\n\n");

        if (swordch0c1 == 'a') {
            printf("\n\n\n\"Well, you see, there's this dragon. He's been causing big problems.     \nHe's destroyed villages, harrassed the priests on the mountain,\n");
            printf("and even attacked a couple cities. His name is Sorrith, and dozens of knights     have already tried to kill him, none of them being successful.\"");
            printf("\nA.\"Say no more, I'll do it.\"\nB.\"Dragon? No way! You better find someone     else.\nC.\"Keep talking...\"");
        }
    }

    return 0;
}

答案 2 :(得分:0)

你在两者之后加上分号...

if(swordch0c1=="b");改变if(swordch0c1=='b') if(swordch0c1=="a");改变if(swordch0c1=='a')

我认为你应该再读一下你的C教程或你的C书,章节说明。

答案 3 :(得分:0)

请勿在if语句后加分号,否则会跳过。始终对char使用单引号,对string使用double。