如果不是这样,我需要帮助理解这个嵌套的问题

I need help understanding the issue with this nested if else

本文关键字:助理 问题 帮助 嵌套 如果不      更新时间:2023-10-16

我正在尝试编写一个简单的程序来区分偶数和奇数并根据它们的值对它们应用一个函数,我尝试运行它,在我的两个"else"语句上都得到了一个"else without before if">

我把它提交给我的班级帮助论坛,他们说用括号包含我的 if 语句,我已经这样做了。(可能不正确(

int output = 0;
if (Input < 0); 
{
cout << "0" << endl:
} else 
{
if (Input % 2 == 0);
{
output = (Input / 2);
cout << output << endl;
}
}
else 
{   if (output = (3 * Input) + 1);
{
cout << output << endl;
}
}

这是错误:

error: ‘else’ without a previous ‘if’ on both of the else statements

如果正确对齐缩进,问题就会变得清晰。

if (Input < 0)
{
cout << "0" << endl:
}
else 
{
if (Input % 2 == 0)
{
output = (Input / 2);
cout << output << endl;
}
}
else 
{
if (output = (3 * Input) + 1)
{
cout << output << endl;
}
}

连续有两else。这毫无意义。

此外,您不应该使用;关闭if ()语句,如果您这样做,您实际上是在告诉编译器此if ()不会触发任何内容(它已被关闭,没有与之关联的代码(。