If else语句c程序中的多个条件

时间:2019-01-17 09:37:05

标签: c

我的程序应该基本上根据用户输入的边的长度来确定三角形的类型。
我收到我的代码的错误消息,该代码进行测试以查看是否为斜角三角形

  

预期为';'在“ {”之前   else((right!= left)||(right!= bottom)||(left!= bottom))

代码:

else((right != left) || (right != bottom) || (left != bottom)){
                printf("This is a scalene triangle");
            }  

错误是说放一个;就在最后一个条件之后,这对我来说没有意义。我试图这样做来测试它,但是它给了我错误的答案。

2 个答案:

答案 0 :(得分:5)

我假设这应该是if...else,在这种情况下,您需要在if之后添加一个额外的else ...

else if ((right != left) || (right != bottom) || (left != bottom))
{
    printf("This is a scalene triangle");
}

响应OP的评论...

  

从笔记中我读到的格式是else的最后一个语句(如果只是其他形式),因此为什么我不使用else if而只是使用else

从某种意义上讲,注释是正确的-最后一条语句可以else语句(如果您有else,则必须为最后一条语句只能有一个)。

因此以下内容是有效的...

if (a == 1) {
   // Do this
} else {
   // Do that
}

但是以下内容无效有效...

if (a == 1) {
   // Do this
} else {
   // Do that
} else {
   // Do other
}

else if允许您继续对多个块进行逻辑处理...如果需要,可以以else块结束...

if (a == 1) {
   // Do this
} else if (b == 1) {
   // Do that
} else {
   // Do other
}

还是不...

if (a == 1) {
   // Do this
} else if (b == 1) {
   // Do that
} else if (c == 1) {
   // Do other
}

答案 1 :(得分:1)

else不能有条件语句,如果需要包括条件检查,则应该为else if

必须进行如下更改

else if ((right != left) || (right != bottom) || (left != bottom)){
                printf("This is a scalene triangle");
            }  
相关问题