问题
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 begin
s and end
s 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 begin
s and 8 end
s.
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 begin
s and end
s. 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 begin
s and end
s 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