本文介绍了致命:语法错误,“."预期但“;"成立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

造成这种情况的原因是你的beginend不平衡;不考虑开头的 begin 和结尾的 end. 为了程序的语法正确,你应该有相同的数字,但是你有 4 个 begins 和 8 ends.

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.

显然,您的代码是计算二次方程的解.我认为您应该做的是调整代码的布局,使其反映这一点,然后正确地设置 begins 和 ends.特别是,您的程序正在尝试检测 a、b 和 d 中的任何一个是否为零,如果是,则编写诊断消息,否则通过常用公式计算根.

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.

不幸的是,您的 begins 和 ends 没有反映这一点.要么需要执行以 d := ... 开头的整个块,要么都不执行,所以前面的 else 需要跟一个begin,如

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;

(你没有说你使用的是哪个 Pascal 编译器,但上面修复了两个在 FreePascal 中被标记为错误的点.

(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.

顺便说一句,在 Pascal 实现中有一些语法结构,其中 end 可以在没有匹配的 begin 之前出现,例如 case ...of ...end.

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

这篇关于致命:语法错误,“."预期但“;"成立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:46