【问题标题】:C programming looping switch case if the it is defaultC编程循环开关盒,如果它是默认的
【发布时间】:2023-03-14 00:13:01
【问题描述】:

我正在为我的布尔表达式使用 switch case,答案是是或否。但如果答案无效,我希望它循环回来。我如何使用 if 或 while 循环与 Character 中的答案可以变化为 'y' 'Y' 'n' 'N'?下面的 if 语句可以工作吗?

if (mstatus != 'y', 'Y', 'N', 'n')
{
    switch(mstatus)
        case 'y':
            printf("You have answer yes for married");
            break;
        case 'Y':
            printf("You have answer yes for married");
            break;
        case 'n':
            printf("You have answer no for married");
            break;
        case 'N':
            printf("You have answer no for married");
            break;
}
else
{
    default:
        printf("please re-enter a valid answer");
        scanf(" %c", &mstatus);
}
return 0;

【问题讨论】:

  • edit问题并正确缩进代码。
  • 一个案例(双关语)用于贯穿开关案例逻辑(如果有的话)。

标签: c loops if-statement switch-statement


【解决方案1】:

switch 语句的一般格式如下:

switch(expression) {

    case constant-expression  :
        statement(s);
        break; /* optional */

    case constant-expression  :
        statement(s);
        break; 
      .
      .
      .

   default : 
       statement(s);
       break;

这意味着default 选项将在没有匹配的情况下被选中。因此,您不需要在 语句中包含 default 大小写。你可以在this link.进一步了解

如果您想连续读取一个字符直到匹配其中一种情况,请将switch 包含在 循环中,如下所示:

int flag = 0;
for (; flag == 1;)
{
    switch(mstatus)
    {
        case 'y':
        case 'Y':
            printf("You have answer yes for married");
            flag = 1;
            break;
        case 'n':
        case 'N':
            printf("You have answer no for married");
            flag = 1;                
            break;
        default:
            printf("please re-enter a valid answer");
            scanf(" %c", &mstatus);
    }
}    

【讨论】:

    【解决方案2】:

    那样的话,你根本不需要if..elseblock。

    如果mstatus 没有y', 'Y', 'N', 'n',则控制将转到default 的情况。

    但是,您可以将switch 语句块放入while(1)/while(someFlag) 循环中,并在您想要中断循环时(取消)设置someFlag .

    【讨论】:

      【解决方案3】:

      您的if 语句不起作用,因为您无法将变量与一系列值进行比较。此外,switch 语句可能是矫枉过正。如果你只是想循环直到得到正确答案,那么你可以这样做:

      for(;;)
      {
        char mstatus;
        printf("please enter a marriage status");
        scanf(" %c", &mstatus);
      
        if(mstatus == 'y' || mstatus =='Y')
        {
          printf("You have answer yes for married");
          break;
        }
        else if(mstatus == 'n' || mstatus =='N')
        {
          printf("You have answer no for married");
          break;
        }
        else
        {
          printf("please re-enter a valid answer");
        }
      }
      

      【讨论】:

        【解决方案4】:

        在您提供的代码中,您根本没有添加循环。您需要添加一个循环才能使其工作。

        此外,如 Saurav 所述,不需要在 switch 语句之上添加 if、then else 语句。

        有很多方法可以做到这一点。一种方法是:

        do
        {
          printf("Answer (y/n)? :");
          scanf(" %c", &mstatus);
          repeat = 0;
          switch(mstatus)
          {
            case 'y':
            case 'Y':
              printf("You have answer yes for married");
              break;
            case 'n':
            case 'N':
              printf("You have answer no for married");
              break;
            default:
              printf("please re-enter a valid answer");
              repeat = 1;
          }
        } while (repeat == 1);
        

        【讨论】:

          【解决方案5】:

          将逻辑包装在一个函数中,这样当你想退出循环时,你可以从函数中返回:

          #include <stdio.h>
          
          int yesno(const char *q)
          {
              char answer = '\0';
          
              while (1) {
                  printf("%s? (y/n) ", q);
                  fflush(stdout);
          
                  scanf(" %c", &answer);
                  switch (answer) {
                  case 'N':
                  case 'n':
                      return 0;
                  case 'Y':
                  case 'y':
                      return 1;
          
                  default:
                      puts("Please answer y or n");
                  }
              }
          
              /* unreachable */
              return -1;
          }
          
          int main(void)
          {
              int yes;
          
              yes = yesno("Are you married");
              printf("You answered %s\n", yes ? "yes" : "no");
              return 0;
          }
          

          【讨论】:

          • 谢谢,您的回答让我对编码有了另一种认识。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-07
          • 1970-01-01
          • 2020-12-06
          相关资源
          最近更新 更多