6

我在第 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;
        }
   }
}
4

4 回答 4

3

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

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

}
于 2013-09-20T11:18:51.423 回答
0

我使用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{} 分支构建程序。

于 2013-09-20T11:25:28.727 回答
0

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

        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;
        }
于 2013-09-20T11:27:29.707 回答
-1

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

于 2013-09-20T11:19:26.623 回答