我用C语言if-else的语句老是出问题,实在是不知道为啥,求解T_T

#include<stdio.h>
int main(void)
{
int A,B,C,D,E,F,G;
printf("提示\n");
while(1);
{
printf("提示1\n");
scanf("%d",&A);
printf("提示2\n");
scanf("%d",&B);
printf("提示3\n");
scanf("%d",&C);
if(C%2==1)
{
D=C-1;
}
E=B+C/2+139;
F=B+C/2+133;
G=B+C/2+127;
if(A==3)
{
printf("%d\n",E);
else if(A=!4);
{
printf("%d\n",F);
else if(A==5);
{
printf("%d\n",G);
}
}
}
else
{
printf("发生错误\n");
return 0;
}
}
return 0;
}
--------------------Configuration: d - Win32 Debug--------------------

Compiling...
e.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\d\e.cpp(24) : error C2181: illegal else without matching if
C:\Program Files\Microsoft Visual Studio\MyProjects\d\e.cpp(25) : warning C4390: ';' : empty controlled statement found; is this the intent?
C:\Program Files\Microsoft Visual Studio\MyProjects\d\e.cpp(27) : error C2181: illegal else without matching if
C:\Program Files\Microsoft Visual Studio\MyProjects\d\e.cpp(28) : warning C4390: ';' : empty controlled statement found; is this the intent?
执行 cl.exe 时出错.

e.obj - 1 error(s), 0 warning(s)
功能主要是要判断输入的变量C的数字是3、4或5进行计算,
问题好像出在if和else的使用错误,求帮忙

if...else if...else...的{}乱套了。每个关键字后面都要用一对{}括起来——
if(...)
{...}
else if(...)
{...}
else if(...)
{...}
else {...}
这样才对。比如你的代码
if(A==3)
{
printf("%d\n",E);
else if(A=!4);
要写成
if(A==3)
{
printf("%d\n",E);
}
else if(A=!4)
{...;}
才正确。追问

谢谢指点,我现在对这些代码的使用不太熟悉,老是出错T_T

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-08-11

不理解你的程序实现什么功能。

单纯的找bug的话。

有一下几点:

1.

else if(A=!4);

这句里面我感觉有三个错误:首先你这个else if 写在了if里面,应该和if并列才对。其次,你这个语句末尾不能有分号“;”。再次,A=!4是想表达啥,如果想表达A不等于4的话应该写成:A!=4


2.

else if(A==5);

跟上面同样的错误。


3.

if语句的用法应该是:
if(condition){

}else if(condition){

}else if(condition){

}...(同上添加else if)
else{

}

追问

谢谢指点

第2个回答  2014-08-11

else if前要有大括号,而不是后面加

else if 条件后面不要分号

#include<stdio.h>

int main(void)
{
 int A,B,C,D,E,F,G;
 printf("提示\n");
 while(1);
 {
  printf("提示1\n");
  scanf("%d",&A);
  printf("提示2\n");
  scanf("%d",&B);
  printf("提示3\n");
  scanf("%d",&C);
  if(C%2==1)
  {
   D=C-1;
  }
E=B+C/2+139;
F=B+C/2+133;
G=B+C/2+127;
if(A==3)
  {
   printf("%d\n",E);
}
else if(A=!4)
   {
    printf("%d\n",F);
}
    else if(A==5)
    {
     printf("%d\n",G);
    }
  else
  {
   printf("发生错误\n");
   return 0;
  }
 }
return 0;
}

改完是这样

追问

谢谢指点

第3个回答  2014-08-11
while(1); 这句去掉分号,否则莫名的死循环。
printf("%d\n",E); 少了}号

后面还很多,确实比较乱。参看White_MouseYBZ说的改。追问

谢谢指点

第4个回答  2014-08-11
else if(A=!4);

这句不能和 if配上对
你把缩进调好了,就能看出来了追问

谢谢指点

第5个回答  2014-08-11
额。。。我建议你用易语言!简单,还是国语追问

易语言不是正式编程用的

追答

关键时刻还得靠它

相似回答