If statement Pascal if/else程序语法错误

If statement Pascal if/else程序语法错误,if-statement,pascal,If Statement,Pascal,我创建了一个程序来确定一个二次方程是否给出了一个“实数”作为其答案,如果是,它是什么。然而,这是我第一次使用if/else,这样我的程序就不会编译超过else,在搜索了半个小时后,我还没有找到原因 代码如下: program Quadratic_Equation_Solver; {$mode objfpc}{$H+} uses Classes, SysUtils, CustApp; var a, b, c : real; begin writeln('Insert th

我创建了一个程序来确定一个二次方程是否给出了一个“实数”作为其答案,如果是,它是什么。然而,这是我第一次使用if/else,这样我的程序就不会编译超过else,在搜索了半个小时后,我还没有找到原因 代码如下:

program Quadratic_Equation_Solver;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, CustApp;
  var
    a, b, c : real;
begin
   writeln('Insert the Value for a please');
   readln(a);
   writeln('Insert the Value for b please');
   readln(b);
   writeln('Insert the Value for c please');
   readln(c);
   if (-4*a*c<b*b) then
      writeln('These variables return an imaginary quantity that');
      writeln('Cannot be computed. Please try again');
      readln;
   (*here it breaks*) else
   Writeln('The Answer is x = ',(-b+sqrt((b*b)-(4*a*c))/(2*a)):8:2);
   readln;
end.
编程二次方程求解器;
{$mode objfpc}{$H+}
使用
类、SysUtils、CustApp;
变量
a、 b,c:真实的;
开始
writeln('请插入值');
readln(a);
writeln('请插入b的值');
readln(b);
writeln('请插入c的值');
readln(c);

if(-4*a*c在
if
else
部分中,似乎缺少了
begin
end
语句。编译器需要这些语句来确定
if
else
代码路径中包含的代码行:

if some condition then
begin
    ...
end
else
begin
    ...
end
因此,在你的情况下:

program Quadratic_Equation_Solver;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, CustApp;
  var
    a, b, c : real;
begin
   writeln('Insert the Value for a please');
   readln(a);
   writeln('Insert the Value for b please');
   readln(b);
   writeln('Insert the Value for c please');
   readln(c);
   if (-4*a*c>b*b) then
   begin
      writeln('These variables return an imaginary quantity that');
      writeln('Cannot be computed. Please try again');
   end
   else
   begin
     Writeln('The Answer is x = ',(-b+sqrt((b*b)-(4*a*c))/(2*a)):8:2);
   end
   readln;
end.

在else语句之前的最后一个语句中不能使用分号

program Quadratic_Equation_Solver;

{$mode objfpc}{$H+}

uses
Classes, SysUtils, CustApp;
var
a, b, c : real;
begin
 writeln('Insert the Value for a please');
 readln(a);
 writeln('Insert the Value for b please');
 readln(b);
 writeln('Insert the Value for c please');
 readln(c);
   if (-4*a*c>b*b) then
    begin
    writeln('These variables return an imaginary quantity that');
    writeln('Cannot be computed. Please try again')
   end (*When using an else statement dont use semicolons*)    
 else 
Writeln('The Answer is x = ',(-b+sqrt((b*b)-(4*a*c))/(2*a)):8:2);
readln;
end.

这是错误的,因为真正的问题是缺少
begin..end
(您也包括了它,但选择不提及),而不是分号(这不是本例中的问题,但您选择了描述)。实际上,您可以在这里使用分号,因为语句将位于
end
之前。