伙计们,我需要编程新手的帮助,并且if-else语句遇到问题
我尝试删除每个包含else / else if的“段落”,但仅适用于if“ paragraph”(显然,因为其他段落不存在)
浮点w,l,r,pi,rec,tri,cir; 字符形状;
printf("What shape is the desired calculated area?\n");
scanf("%c",&shape);
if(shape=='R')
printf("width: ");
scanf("%f",&w);
printf("length: ");
scanf("%f",&l);
rec=w*l;
printf("\nThe area for the rectangle is %.2f Cm2\n",rec);
else if(shape=='C')
printf("radius: ");
scanf("%f",&r);
pi=3.142;
cir=pi*r*r;
printf("\nThe area for the rectangle is %.2f Cm2\n",cir);
else if(shape=='T')
printf("width: ");
scanf("%f",&w);
printf("length: ");
scanf("%f",&l);
tri=1/2*w*l;
printf("\nThe area for the rectangle is %.2f Cm2\n",tri);
else
printf("You must choose one type of shape using R C T only and only digits are allowed afterwards");
我收到一条消息,提示“ else”之前每行包含else单词的行都解析为[Error]解析错误。进一步阅读错误消息后会显示错误:“ else”,而之前没有“ if”。任何帮助将不胜感激
答案 0 :(得分:2)
如果您使用“ if”时一行超过一个,则使用{} 喜欢
if(shape=='R'){
printf("width: ");
scanf("%f",&w);
printf("length: ");
scanf("%f",&l);
rec=w*l;
printf("\nThe area for the rectangle is %.2f Cm2\n",rec);
}
if(shape=='R')
printf("width: ");
scanf("%f",&w);
printf("length: ");
scanf("%f",&l);
rec=w*l;
printf("\nThe area for the rectangle is %.2f Cm2\n",rec);
会像下面的编译器中一样
if(shape=='R'){
printf("width: ");
}
scanf("%f",&w);
printf("length: ");
scanf("%f",&l);
rec=w*l;
printf("\nThe area for the rectangle is %.2f Cm2\n",rec);