【问题标题】:Switch case help in CC中的切换案例帮助
【发布时间】:2013-09-20 11:05:42
【问题描述】:

我在 9 年级,所以仍然是 C 的初学者。谁能告诉我该怎么做?当任何人输入大于 4 的值时,它应该打印 switch case 语句的“默认:”标签。我尝试使用 do while ,但它给出了错误。代码是

#include <stdio.h>
#include <unistd.h>
void main()
{
   int n1,n2,a=0,c,r,o;
S:
   printf("\n \n 1. Addition \n 2. Substract \n 3. Multiply \n 4. Divide \n \n");
   printf("\n Enter your choice: \t");
   scanf("%d",&o);
   printf("Enter two numbers: \t");
   scanf("%d %d",&n1,&n2);

   switch (o)
   {
      case 1:
         a=n1+n2;
         printf("\n Please wait..");
         sleep(1);
         printf("\n Answer is %d",a);
         printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
         scanf("%d",&c);
         if (c==1)
         {
            goto S;
         }
         if (c==0)
         {
            printf("\n \n \n Bye!");
         }
         else
         {
            printf("Choice ain't correct!");
         }
L:
         printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
         scanf("%d",&r);
         if (r==1)
         {
            printf("\n \n Restarting Loop..");
            sleep(1);
            goto S;
         }
        else
        {
           printf("\n \t \t \t Bye!");
           goto L;
        }
        break;

      case 2:
        a=n1-n2;
        printf("\n Please wait..");
        sleep(1);
        printf("\n Answer is %d",a);
        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);
        if (c==1)
        {
           goto S;
        }
        if (c==0)
        {
           printf("\n \n \n Bye!");
        }
        else
        {
           printf("Choice ain't correct!");
        }
M:
        printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
        scanf("%d",&r);
        if (r==1)
        {
           printf("\n \n Restarting Loop..");
           sleep(1);
           goto S;
        }
        else
        {
           printf("\n \t \t \t Bye!");
           goto M;
        }
        break;

      case 3:
        a=n1*n2;
        printf("\n Please wait..");
        sleep(1);
        printf("\n Answer is %d",a);
        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);
        if (c==1)
        {
           goto S;
        }
        if (c==0)
        {
           printf("\n \n \n Bye!");
        }
        else
        {
           printf("Choice ain't correct!");
        }
  N:
        printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
        scanf("%d",&r);
        if (r==1)
        {
           printf("\n \n Restarting Loop..");
           sleep(1);
           goto S;
        }
        else
        {
           printf("\n \t \t \t Bye!");
           goto N;
        }
        break;

      case 4:
        a=n1/n2;
        printf("\n Please wait..");
        sleep(1);
        printf("\n Answer is %d",a);
        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);
        if (c==1)
        {
           goto S;
        }
        if (c==0)
        {
           printf("\n \n \n Bye!");
        }
        else
        {
           printf("Choice ain't correct!");
        }
O:
        printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
        scanf("%d",&r);
        if (r==1)
        {
           printf("\n \n Restarting Loop..");
           sleep(1);
           goto S;
        }
        else
        {
           printf("\n \t \t \t Bye!");
           goto O;
           break;
      default:
              printf("Choice ain't correct");
              break;
        }
   }
}

【问题讨论】:

  • 欢迎来到 StackOverflow ! 2个提示:1)你应该缩进你的代码,使其更具可读性。 2) goto 是邪恶的。
  • @Arnaud Denoyelle 这是一个谎言,如果你不能遵守标准,goto 是邪恶的。但是,每种语言结构都可能是邪恶的。
  • Elazar 那么我怎样才能在我的代码中使用 goto 之类的其他东西呢? Hagubear 但是程序给出了正确的输出。
  • 比空格和 goto 更重要:你得到什么错误?
  • 您可以将方法中的代码片段分开提取。这是最干净的方式恕我直言。

标签: c loops switch-statement


【解决方案1】:

您的代码确实很糟糕,原因在您帖子中的 cmets 中说明;但我认为你应该通过改进自己的代码来学习,这就是我早期的帮助:

  1. 在要求用户输入两个值之前,您应该检查第一个输入(使用您的开关)。这就是程序逻辑
  2. 在默认情况下,添加一个 goto O;甚至转到 S;

    默认值: printf("选择不正确。再试一次..\n"); 转到O; 休息;

  3. 你可能想在你说再见后终止程序 - 对我来说更合理

  4. 我真的真的非常建议重构代码以在没有 go-to 的情况下运行。这是一个非常好的任务,可以通过一个循环来解决。

您具体指的是哪些错误?

~编辑~

我想我会用一些代码来说明我的意思,这就是您的程序有多简单,希望对您有所帮助,您不要只是复制代码,而是要尝试理解为什么它“更好”(至少更短并且比你的更容易维护和阅读);)

#include <stdio.h>
#include <unistd.h>


int main()
{
    int n1,n2,a=0,c,o;

    int terminate = 0;

    while(!terminate)
    {
        printf("\n \n 1. Addition \n 2. Substract \n 3. Multiply \n 4. Divide \n \n");
        printf("\n Enter your choice: \t");
        scanf("%d",&o);

        if(o < 0 || o> 4)
        {
             printf("Choice ain't correct!\n");
             continue; // restarts loop
        }

        printf("Enter two numbers: \n ");
        scanf("%d %d",&n1,&n2);

        switch(o)
        {
            case 1: a = n1 + n2;
                break;

            case 2: a = n1-n2;
                break;

            case 3: a = n1*n2;
                break;

            case 4: a = n1/n2;
                break;

            default:
                // never reached, since validation of o was done before switch
                break;
        }

        sleep(1);
        printf("\n Answer is %d",a);



        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);

        if (c!=1)
        {
            terminate = 1; // this causes the loop to terminate
        }
    }

    printf("\n \n \n Bye!");

}

【讨论】:

  • 在我看来,鼓励使用 goto 并没有帮助。是的,它有它的位置,但它的位置绝对不在初学者的程序中。
  • 这就是我添加代码示例的原因,我还记得我在这个阶段的时候(那时我14岁)。我们被告知“嘿,有类似 goto 的东西,但不要使用它,它更像是汇编语言的遗留物,而不是真正为 c 编程而设计的”。在汇编程序中它是有道理的,在 C 中它不是真的(在我看来)。杰森,不幸的是,我现在很忙,所以没有时间帮助你,但我很确定你可以自己完成。当我弄清楚自己时,我总是学到最多(这需要很多时间..)。否则可能只是问你的朋友或老师?
【解决方案2】:

我使用 while 循环的解决方案:

int main(){
    int o, a1, a2;
    printf("Hi!\r\n");
    while(1){
        printf("1234 - operations, else - exit\r\n");
        scanf("%d", &o);
        if(o < 1 || o > 4)
            break; /* breaks infinite while loop and goes straight to "bye!" */
        printf("Enter args: ");
        scanf("%d %d", &a1, &a2);
        switch(o){
        /* here your switchcase statements without default
        just make opearions and output result */
        }
    }
    printf("Bye!\r\n");
    return 0;
}

Goto 运算符是危险的,不是违法的,但你必须明智地使用它。 尝试先使用循环和 if(){}else{} 分支构建程序。

【讨论】:

    【解决方案3】:

    在默认情况下,大括号不在正确的位置

            if (r==1)
            {
               printf("\n \n Restarting Loop..");
               sleep(1);
               goto S;
            }
            else
            {
               printf("\n \t \t \t Bye!");
               goto O;
               break;
            }  // <------------------------------------------------------- Need this
    
          default:
            {   // <-----------------------------------------------------  Need this
                  printf("Choice ain't correct");
                  break;
            }
    

    【讨论】:

      【解决方案4】:

      您在 default: case 之前缺少一个右大括号,这就是它没有执行的原因。

      【讨论】:

      • 评论不准确。他可能的意思是把默认从 else 子句中移出。因此,如果您在默认前添加右大括号,则必须在默认后删除右大括号。尽管我仍然会争辩说它并没有达到问题中首先提出的要求。在要求输入接下来的两个数字之前,您确实需要测试您的第一个输入。
      • 评论非常准确。他问为什么他的default: 案件没有执行,这就是原因。他的代码包含其他错误的事实是或应该是另一个问题的主题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 2011-08-17
      • 2011-10-07
      相关资源
      最近更新 更多