Pascal if / else程序语法错误

时间:2014-09-13 20:25:42

标签: if-statement pascal

我创建了一个程序来确定二次方程是否给出了一个“真实”数字作为答案,如果是,它是什么。然而,这是我第一次使用if / 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.

在休息时它说它需要一个分号但是没有用

2 个答案:

答案 0 :(得分:4)

您似乎错过了beginend部分中的ifelse声明。编译器需要这些来确定ifelse代码路径中包含的代码行:

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.

答案 1 :(得分:0)

在你的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.