问题描述
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.
推荐答案
造成这种情况的原因是你的begin
和end
不平衡;不考虑开头的 begin
和结尾的 end.
为了程序的语法正确,你应该有相同的数字,但是你有 4 个 begin
s 和 8 end
s.
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.
显然,您的代码是计算二次方程的解.我认为您应该做的是调整代码的布局,使其反映这一点,然后正确地设置 begin
s 和 end
s.特别是,您的程序正在尝试检测 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 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.
不幸的是,您的 begin
s 和 end
s 没有反映这一点.要么需要执行以 d := ...
开头的整个块,要么都不执行,所以前面的 else
需要跟一个begin
,如
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;
(你没有说你使用的是哪个 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
.
这篇关于致命:语法错误,“."预期但“;"成立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!