是的& C中没有答案

时间:2017-08-25 03:31:57

标签: c

我正在尝试创建一个C程序,我可以向以下用户询问是或否:

Do you like beer? Y or N

Are you old enough? Y or N

How old are you?

如果用户说超过18,则打印消息:let's go for some beers

但如果前两个问题之一或年龄为N,请打印:sorry maybe next time

我认为问题在于If语句,也许我没有封装条件。

#include <stdio.h>

int main(){

    char answer;
    int age = 0 ;

    printf("Do you like beers Enter Y or N: \n");
    scanf(" %c", &answer);
    printf("\n so your answer is %c\n", answer);

    printf("Are you old enough to have a beer?\n");
    scanf(" %c", &answer);
    printf("\n so your answer is %c\n", answer);


    if (answer == 'Y') {

        printf("how old are you?\n");
        scanf(" %d", &age);

        if (age >= 18)
            printf("\n Let's go for some beers , I will pay the first round \n");

    }if else  (age < 18 && answer == 'N')
        printf("\n sorry my friend , maybe next time \n");



       // printf("You may NOT ");

  return 0;

}

4 个答案:

答案 0 :(得分:3)

从上面的代码段开始,看起来你的else语句格式不正确(if else而不是else if)。此外,由于您正在测试 问题是否为false,因此您应该使用||操作数。所以你想要做的事情如下:

else if  (age < 18 || answer == 'N')

答案 1 :(得分:0)

问题是你的最后一个if语句只有在用户未满18岁时才为真,并且说你想要它是否。或者

更改最后一个if语句
time(h:i:s)

致:

if else  (age < 18 && answer == 'N')

答案 2 :(得分:0)

您正在使用相同的变量“answer”。因此,当您输入第二个问题的答案时,它将替换第一个答案。

    int main(){

        char answer;
        char answer2;
        int age = 0 ;

        printf("Do you like beers Enter Y or N: \n");
        scanf(" %c", &answer);
        printf("\n so you answer is %c\n", answer);

        printf("Are you older enough to have a beer?\n");
        scanf(" %c", &answer2);
        printf("\n so you answer is also %c\n", answer2);


        if (answer == 'Y' && answer2 == 'Y') {

            printf("how old are yo.\n");
            scanf(" %d", &age);

            if (age >= 18)
                printf("\n lets go for some beers , I will paid the first round \n");

        }

        else if  (answer == 'N' || answer2 == 'N')
            printf("\n sorry my friend , maybe next time \n");

           // printf("You may NOT ");

      return 0;

    }

希望这是你所需要的。欢呼声。

答案 3 :(得分:0)

我认为您可以认为程序的结构大致如下。

REPLY = PROMPT(Q1_MESSAGE)
IF(REPLY == YES){
    //Narrow down the conditions
    REPLY = PROMPT(Q2_MESSAGE)
    IF(REPLY == YES){
        //Narrow down the conditions
        REPLY = PROMPT(Q3_MESSAGE)
        IF(REPLY >= 18){
            DISPLAY(GOOD_MESSAGE)
        } ELSE {
            DISPLAY(NO_GOOD_MESSAGE)
        }
    } ELSE {
        DISPLAY(NO_GOOD_MESSAGE)
    }
} ELSE {
    DISPLAY(NO_GOOD_MESSAGE)
}

嵌套IF可以被视为AND条件作为条件 因此,通过将问题消息及其响应部分汇总到函数中,可以编写如下。

IF(FUNC1() == TRUE AND FUNC2() == TRUE AND FUNC3() == TRUE){//Function call proceeds from left to right (Shortcut evaluated)
    DISPLAY(GOOD_MESSAGE)
} ELSE {
    DISPLAY(NO_GOOD_MESSAGE)
}

作为一个例子,你可以具体写出如下。

#include <stdio.h>

int main(void){
    char YN(const char *prompt);
    int enter_age(const char *prompt);

    if(YN("Do you like beers Enter Y or N: \n") == 'Y' &&
       YN("Are you old enough to have a beer?\n") == 'Y' &&
       enter_age("How old are you?\n") >= 20){
        printf("\nLet's go for some beers, I will take the first round.\n");
    } else {
        printf("\nSorry my friend, maybe next time.\n");
    }

    return 0;
}

char YN(const char *prompt){
    char ans[2], ret, ch;
    int ret_scnf;

    while(1){
        fputs(prompt, stdout);
        if((ret_scnf = scanf(" %1[YNyn]%c", ans, &ch)) == 2 && ch == '\n'){
            if(*ans == 'Y' || *ans == 'y'){
                ret = 'Y';
                break;
            } else if(*ans == 'N' || *ans == 'n'){
                ret = 'N';
                break;
            }
        } else if(ret_scnf == EOF){
                ret = 'N';
                break;
        }
        scanf("%*[^\n]");scanf("%*c");//clear input
    }
    return ret;
}
int enter_age(const char *prompt){
    int ret_scnf, age;
    char ch;

    while(1){
        fputs(prompt, stdout);
        if((ret_scnf = scanf("%d%c", &age, &ch)) == 2 && ch == '\n'){
            if(age < 0)//Can I enter years old at the age of 0?
                continue;
            return age;
        } else if(ret_scnf == EOF){
                age = -1;
                break;
        }
        scanf("%*[^\n]");scanf("%*c");
    }
    return age;
}