当我尝试编译代码时,出现错误else without a previous if
:
// Fibonacci series using recursion
#include <iostream>
using namespace std;
int fib (int n);
int main()
{
int n, answer;
cout << "\n\n\t\tEnter number to find: ";
cin >> n;
cout << "\n\n";
answer = fib(n);
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
{
if(n < 3)
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
else
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
else
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
return 0;
}
int fib (int n)
{
cout << "Processing fib (" << n << ")... ";
if (n < 3)
{
cout << "Return 1!\n";
return 1;
}
else
{
cout << "Call fib(" << n-2 << ") ";
cout << "and fib(" << n-1 << ").\n";
return( fib(n-2) + fib(n-1));
}
}
答案 0 :(得分:4)
肯定是关键括号的问题:
int main()
{
int n, answer;
cout << "\n\n\t\tEnter number to find: ";
cin >> n;
cout << "\n\n";
answer = fib(n);
if(n < 3 && n > 1) {
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
if(n < 3) {
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
} else {
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
}
else {
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
}
return 0;
}
答案 1 :(得分:3)
你的if
之后缺少荣誉(花括号)if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
{
if(n < 3)
装置
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
} // end of if
cout << "nd Fibonacci number\n"; // always executed
{ // new anonymous block
if(n < 3)
答案 2 :(得分:2)
(编辑:这是一个完全重做的答案。请参阅评论。谢谢你,抱歉“争议”。)
因为您未在{
中正确使用大括号(“大括号”,}
和main
)。
首先让我们来看看代码的内部部分:
if(n < 3)
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
else
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
目前的缩进是“错误的”并且具有误导性。如果将其复制粘贴到代码编辑器中并使用自动格式化(auto- 缩进就足够了),您将获得:
if(n < 3)
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
else
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
它向您展示了代码的真正“含义”。为了清晰起见,在添加括号和空白行之后:
if(n < 3)
{
cout << answer << " is the " << n;
}
cout << "st Fibonacci number\n";
else
{
cout << answer << " is the " << n;
}
cout << "rd Fibonacci number\n";
如您所见,cout
仅限第一个if
语句。第二个将永远执行。然后是一个else
,它遵循“普通”,“无条件”语句,而不是“条件”语句/块(一个语句块整体上也是一个语句 )。
要修复此部分,必须将所有条件语句包装在大括号中:
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
或更紧凑的风格:
if(n < 3) {
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
} else {
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
使完整的块语句受到限制。
现在“内部”if-else部分已修复,让我们采用“外部”if-else:
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
{
/* ... fixed inner if-else ... */
}
else
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
让我们再次使用代码格式化程序:
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
{
/* ... fixed inner if-else ... */
}
else
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
现在应该清楚真正的意义(在这里使用紧凑的风格):
if(n < 3 && n > 1) {
cout << answer << " is the " << n;
}
cout << "nd Fibonacci number\n";
{
/* ... fixed inner if-else ... */
}
else {
cout << answer << " is the " << n;
}
cout << "th Fibonacci number\n";
中间的滑稽块(括号内的代码但不是直接跟在if
/ else
之后)实际上是一个匿名块,它只引入了一个内部范围(内部定义的变量不存在)结束后}
)。它可以看作是一个简单的陈述(无条件),就像它上面的cout << "nd Fibonacci number\n";
一样。
再一次,修复很明显:
if(n < 3 && n > 1) {
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
/* ... fixed inner if-else ... */
} else {
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
}
答案 3 :(得分:2)
试试这个:
int main()
{
int n, answer;
cout << "\n\n\t\tEnter number to find: ";
cin >> n;
cout << "\n\n";
answer = fib(n);
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
}
else
{
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
}
return 0;
}
确保您的if
和else
因此在大括号内。
答案 4 :(得分:2)
您没有在带有多个语句的if else子句中添加括号。不要在现实世界的编码中这样做。 更改代码如下:
// Fibonacci series using recursion
#include <iostream>
using namespace std;
int fib (int n);
int main()
{
int n, answer;
cout << "\n\n\t\tEnter number to find: ";
cin >> n;
cout << "\n\n";
answer = fib(n);
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
}
else {
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
}
return 0;
}
int fib (int n)
{
cout << "Processing fib (" << n << ")... ";
if (n < 3)
{
cout << "Return 1!\n";
return 1;
}
else
{
cout << "Call fib(" << n-2 << ") ";
cout << "and fib(" << n-1 << ").\n";
return( fib(n-2) + fib(n-1));
}
}
答案 5 :(得分:1)
你忘了在内部if上使用{}。它应该是
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
答案 6 :(得分:1)
我相信你在if(n&lt; 3)之后缺少大括号,因此条件仅适用于下面的行。然后编译器命中'else'......
答案 7 :(得分:0)
关于花括号的所有答案都是正确的,但我需要的代码就像这些人一样:
if (n > 3)
{
cout << answer << " is the " << n;
cout << "th Fibonacci number\n";
}
else
{
if(n == 3)
{
cout << answer << " is the " << n;
cout << "rd Fibonacci number\n";
}
else
{
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
cout << "nd Fibonacci number\n";
}
else
{
cout << answer << " is the " << n;
cout << "st Fibonacci number\n";
}
}
}