簡體   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