我在第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;
}
}
}
答案 0 :(得分:3)
您的代码非常糟糕,您帖子中的评论中说明的原因;但我认为你应该通过改进自己的代码来学习,这是我早期的帮助:
在默认情况下,添加一个转到O;甚至转到S;
默认: printf(“选择不正确。再试一次.. \ n”); 转到O; 打破;
你说再见之后你可能想终止节目 - 对我来说更合理
我真的非常推荐重构代码,以便在没有首选的情况下运行。它是一个非常好的任务,可以用一个循环来解决它。
您的意思是什么错误?
〜编辑〜
我想我会用一些代码来说明我的意思,这就是你的程序有多简单,希望有所帮助,你不要只是复制代码,而是试着理解为什么那些“更好”(至少更短)并且比你的更容易维护和阅读;)
#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!");
}
答案 1 :(得分: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;
}
转到操作员是危险的,不是违法的,但你必须明智地使用它。 尝试使用循环构建程序,if(){} else {}首先分支。
答案 2 :(得分: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;
}
答案 3 :(得分:-1)
你在默认情况之前错过了一个右大括号:这就是它没有执行的原因。