Fatal: Syntax error, “.” expected but “;” found

被刻印的时光 ゝ 提交于 2020-05-17 10:38:41

问题


program Hello;
var

a,b,c,x,d: integer;
x1,x2: real;

begin

readln(a,b,c);

if a = 0 then
begin
    if b = 0 then
    begin
        if c = 0 then
        begin
            writeln('11');
        end
        else 
            writeln('21');
        end;
    end
    else
        writeln('31');
    end;
end
else
    d := b^2 - 4*a*c;

    if d < 0 then
    begin
        writeln('Нет Вещественных корней!');
    end
    else
        x1 := (-b + sqrt(d))/(2*a);
        x2 := (-b - sqrt(d))/(2*a);
        writeln('Первый Корень:' + x1 + ' ' + 'Второй Корень:' + x2);
    end;
end;
end.

回答1:


The reason for this is that your begins and ends are not balanced; disregarding the opening begin and closing end. for the program's syntax to be correct, you should have equal numbers of each, but you have 4 begins and 8 ends.

Obviously, your code is to compute the solutions of a quadratic equation. What I think you should do is to adjust the layout of your code so that it reflects that and then correctly the begins and ends. In particular, your program is trying to detect whether any of a, b and d is zero and, if so, write a diagnostic message, otherwise calculate the roots by the usual formula.

Unfortunately, your begins and ends do not reflect that. Either the whole of the block starting d := ... needs to be executed or none of it does, so the else on the line before needs to be followed by a begin, as in

  else begin
    d := b*b - 4*a*c;  //b^2 - 4*a*c;
    if d < 0 then begin
      writeln('Нет Вещественных корней!');
    end
    else begin
      x1 := (-b + sqrt(d))/(2*a);
      x2 := (-b - sqrt(d))/(2*a);
//      writeln('Первый Корень:' + x1 + ' ' + 'Второй Корень:' + x2);
      writeln('Первый Корень:', x1, ' Второй Корень:' , x2);
    end;
  end;

(You don't say which Pascal compiler you are using, but the above fixes two points which are flagged as errors in FreePascal.

If you need more help than that, please ask in a comment.

Btw, there are some grammatical constructs in Pascal implementations where an end can appear without a matching preceding begin such as case ... of ...end.



来源:https://stackoverflow.com/questions/61205196/fatal-syntax-error-expected-but-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!