我的程序应该根据用户输入的边长确定三角形的类型。
我的代码收到此错误消息,用于测试它是否是不等边三角形
预期的 ';' '{' else((right != left) || (right != bottom) || (left != bottom)){
代码:
else((right != left) || (right != bottom) || (left != bottom)){
printf("This is a scalene triangle");
}
错误是说要放一个 ; 在最后一个条件之后,这对我来说没有意义。我试图这样做来测试它,但它给了我错误的答案。
我假设这应该是一个if...else
在这种情况下你需要一个额外if
的else
......
else if ((right != left) || (right != bottom) || (left != bottom))
{
printf("This is a scalene triangle");
}
为了回应OP的评论......
从我正在阅读的笔记中,格式是 else if 的最后一条语句,因此为什么我没有使用 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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句