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