为什么我会得到一个编译错误,上面写着error:“else”而没有前面的“if”

Why do I get a compiling error that says error: ‘else’ without a previous ‘if’?

本文关键字:else error if 前面 错误 编译 一个 为什么      更新时间:2023-10-16

当我试图编译代码时,我收到一个错误,上面写着else而没有前面的if

// Fibonacci series using recursion
#include <iostream>
using namespace std;
int fib (int n);
int main()
{
     int n, answer;
     cout << "nnttEnter number to find: ";
     cin >> n;
     cout << "nn";
     answer = fib(n);
     if(n < 3 && n > 1)
         cout << answer << " is the " << n;
         cout << "nd Fibonacci numbern";
     {
         if(n < 3)
             cout << answer << " is the " << n;
             cout << "st Fibonacci numbern";
         else
             cout << answer << " is the " << n;
             cout << "rd Fibonacci numbern";
     }
     else
         cout << answer << " is the " << n;
         cout << "th Fibonacci numbern";
     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 << "nnttEnter number to find: ";
     cin >> n;
     cout << "nn";
     answer = fib(n);
     if(n < 3 && n > 1) {
         cout << answer << " is the " << n;
         cout << "nd Fibonacci numbern";
         if(n < 3) {
             cout << answer << " is the " << n;
             cout << "st Fibonacci numbern";
         } else {
             cout << answer << " is the " << n;
             cout << "rd Fibonacci numbern";
         }
     }
     else {
         cout << answer << " is the " << n;
         cout << "th Fibonacci numbern";
     }
     return 0;
}

如果:,您的后将缺少荣誉(花括号)

if(n < 3 && n > 1)
    cout << answer << " is the " << n;
    cout << "nd Fibonacci numbern";
{
    if(n < 3)

 if(n < 3 && n > 1)
 {
     cout << answer << " is the " << n;
 } // end of if
 cout << "nd Fibonacci numbern"; // always executed
 { // new anonymous block
     if(n < 3)

试试这个:

int main()
{
     int n, answer;
     cout << "nnttEnter number to find: ";
     cin >> n;
     cout << "nn";
     answer = fib(n);
     if(n < 3 && n > 1) 
     {
         cout << answer << " is the " << n;
         cout << "nd Fibonacci numbern";
         if(n < 3) 
         {
             cout << answer << " is the " << n;
             cout << "st Fibonacci numbern";
         } 
         else 
         {
             cout << answer << " is the " << n;
             cout << "rd Fibonacci numbern";
         }
     }
     else
     {
         cout << answer << " is the " << n;
         cout << "th Fibonacci numbern";
     }
     return 0;
}

请确保您的ifelse相应地位于花括号内。

您没有在带有多个语句的if-else子句中添加方括号。不要在现实世界中进行编码。更改代码如下:

// Fibonacci series using recursion
#include <iostream>
using namespace std;
int fib (int n);
int main()
{
     int n, answer;
     cout << "nnttEnter number to find: ";
     cin >> n;
     cout << "nn";
     answer = fib(n);
     if(n < 3 && n > 1)
      {
         cout << answer << " is the " << n;
         cout << "nd Fibonacci numbern";
      if(n < 3)
     {
         cout << answer << " is the " << n;
         cout << "st Fibonacci numbern";
     }
      else   
      {
         cout << answer << " is the " << n;
         cout << "rd Fibonacci numbern";
       }
     }
     else {
     cout << answer << " is the " << n;
     cout << "th Fibonacci numbern";
     }
     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));
     }
}

因为在main中没有正确使用大括号("花括号"、{})。

首先,我们来看一下代码的内部部分:

      if(n < 3)
         cout << answer << " is the " << n;
         cout << "st Fibonacci numbern";
      else
         cout << answer << " is the " << n;
         cout << "rd Fibonacci numbern";

当前的缩进是";错误的";以及误导。如果您将其复制粘贴到代码编辑器中并使用自动格式化(自动缩进就足够了),您将得到:

      if(n < 3)
         cout << answer << " is the " << n;
      cout << "st Fibonacci numbern";
      else
         cout << answer << " is the " << n;
      cout << "rd Fibonacci numbern";

它向你展示了真实的";意思是";代码的。在添加大括号和空行以提高清晰度后:

      if(n < 3)
      {
         cout << answer << " is the " << n;
      }
      cout << "st Fibonacci numbern";
      else
      {
         cout << answer << " is the " << n;
      }
      cout << "rd Fibonacci numbern";

正如您所看到的,只有第一个cout语句受到if的制约。第二个将始终执行。接着是CCD_ 8;"普通"无条件的";声明,而不是";条件";语句/块(一个语句块作为一个整体也是一个语句)。

要修复这一部分,必须用大括号包装所有条件语句:

      if(n < 3)
      {
         cout << answer << " is the " << n;
         cout << "st Fibonacci numbern";
      }
      else
      {
         cout << answer << " is the " << n;
         cout << "rd Fibonacci numbern";
      }

或者更紧凑的风格:

      if(n < 3) {
         cout << answer << " is the " << n;
         cout << "st Fibonacci numbern";
      } else {
         cout << answer << " is the " << n;
         cout << "rd Fibonacci numbern";
      }

使得完整的块语句是有条件的。

现在;内部";如果else部分是固定的,让我们取";外部";如果其他:

     if(n < 3 && n > 1)
         cout << answer << " is the " << n;
         cout << "nd Fibonacci numbern";
     {
      /* ... fixed inner if-else ... */
     }
     else
     cout << answer << " is the " << n;
     cout << "th Fibonacci numbern";

让我们再次使用代码格式化程序:

     if(n < 3 && n > 1)
         cout << answer << " is the " << n;
     cout << "nd Fibonacci numbern";
     {
         /* ... fixed inner if-else ... */
     }
     else
         cout << answer << " is the " << n;
     cout << "th Fibonacci numbern";

真正的含义现在应该很清楚了(这里使用紧凑风格):

     if(n < 3 && n > 1) {
         cout << answer << " is the " << n;
     }
     cout << "nd Fibonacci numbern";
     {
         /* ... fixed inner if-else ... */
     }
     else {
         cout << answer << " is the " << n;
     }
     cout << "th Fibonacci numbern";

中间单独的有趣块(大括号内的代码,但不直接在if/else后面)实际上是一个匿名块,它只引入了一个内部范围(内部定义的变量在关闭}后将不再存在)。它可以被看作是一个简单的语句(无条件的),就像它上面的cout << "nd Fibonacci numbern";一样

再一次,修复是显而易见的:

     if(n < 3 && n > 1) {
         cout << answer << " is the " << n;
         cout << "nd Fibonacci numbern";
         /* ... fixed inner if-else ... */
     } else {
         cout << answer << " is the " << n;
         cout << "th Fibonacci numbern";
     }

您忘记在内部if上使用{}。应该是

if(n < 3)
{
   cout << answer << " is the " << n;
   cout << "st Fibonacci numbern";
}
else
{   
    cout << answer << " is the " << n;
    cout << "rd Fibonacci numbern";
}

我相信if(n<3)后面缺少大括号,所以条件仅适用于下面的行。然后编译器点击"else"。。。。。。

关于花括号的所有答案都是正确的,但我需要的代码是这样的:

if (n > 3)
{
    cout << answer << " is the " << n;
    cout << "th Fibonacci numbern"; // E.g., "5th Fibonacci number"
}
else
{
    if(n == 3)
    {
        cout << answer << " is the " << n;
        cout << "rd Fibonacci numbern";  // "3rd Fibonacci number"
    }
    else
    {
       if(n < 3 && n > 1)
       {
           cout << answer << " is the " << n;
           cout << "nd Fibonacci numbern"; // "2nd Fibonacci number"
       }
       else
       {
           cout << answer << " is the " << n;
           cout << "st Fibonacci numbern"; // "1st Fibonacci number"
       }
    }
}