如果不是这样,我需要帮助理解这个嵌套的问题
I need help understanding the issue with this nested if else
我正在尝试编写一个简单的程序来区分偶数和奇数并根据它们的值对它们应用一个函数,我尝试运行它,在我的两个"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 ()
不会触发任何内容(它已被关闭,没有与之关联的代码(。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题