在C中切换案例帮助

Jas*_*ark 6 c loops switch-statement

我在9年级,所以仍然是C的初学者.谁能说出如何做到这一点?当任何人输入超过4的值时,它应该打开switch case语句的'default:'标签.我尝试使用do for 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;
        }
   }
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*ler 3

你的代码真的很糟糕,原因在你的帖子的评论中说明;但我认为你应该通过改进自己的代码来学习,这对我早期很有帮助:

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

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

  3. 你可能想在你说再见后终止计划——对我来说更合理

  4. 我真的真的建议重构代码以使其无需执行即可运行。这是一个非常好的任务,可以用一个循环来解决它。

您具体指的是哪些错误?

~编辑~

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

#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!");

}
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,鼓励使用 goto 没有帮助。是的,它有它的位置,但它的位置绝对不在初学者的程序中。 (4认同)