繁体   English   中英

如何在此 C 代码中正确使用开关?

[英]How can I correctly use switch in this C code?

我想使用 switch 语句来检查用户的回复是 T (true) 还是 F(false)。 但它不会问用户“是真是假?”这个问题。 我应该在这段代码中改变什么?

#include <stdio.h>
#include <stdlib.h>
int main(){
      char name[15];
      int age;
      double date_of_birth;
      char reply;
      printf("Enter your name: ");
      fgets(name, 15, stdin);
      printf("Enter your age: ");
      scanf("%d", &age);
      printf("Enter your date of birth: ");
      scanf("%lf", &date_of_birth);
      printf("Hello, %sYour age is %d and your date of birth is %f\n", name, age, date_of_birth);
      printf("Is it true or false? ");
      scanf("%c", &reply);
      switch(reply){
      case 'T':
            printf("You was added.");
      case 'F':
            printf("Enter your information again.");
      }

      return 0;
}

不要忘记在案例末尾添加 break 语句,否则您的 switch 将执行触发的案例之下的所有可用案例。

在 switch-case 块中的每个 case 之后添加 break 语句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM