为什么我得到一个编译错误,说错误:“else”没有以前的“if”? [英] Why do I get a compiling error that says error: ‘else’ without a previous ‘if’?
本文介绍了为什么我得到一个编译错误,说错误:“else”没有以前的“if”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试编译代码时,我得到一个错误,说 else没有前一个
:
When I try to compile the code I get an error that says 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));
}
}
推荐答案
它是关键括号的问题:
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;
}
这篇关于为什么我得到一个编译错误,说错误:“else”没有以前的“if”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文