c/c++语言开发共享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  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 %cn", answer); printf("Are you old enough to have a beer?n"); scanf(" %c", &answer); printf("n so your answer is %cn", 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; } 

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

    从中更改最后一个if语句

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

    至:

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

    从上面的代码片段看,你的else语句看起来格式不正确( if else代替else if )。 此外,由于您正在测试这两个问题是否都是错误的,您应该使用|| 操作数。 所以你想要做的事情如下:

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

    您正在使用相同的变量“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 %cn", answer); printf("Are you older enough to have a beer?n"); scanf(" %c", &answer2); printf("n so you answer is also %cn", 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; } 

    希望这是你需要的。 干杯。

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

     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  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; } 

      以上就是c/c++开发分享C中是,否答案相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/549956.html

      (0)
      上一篇 2021年1月14日
      下一篇 2021年1月14日

      精彩推荐