我正在使用pascal进行分配,但一直遇到此错误'“;”预期,但其他人发现了。我已经看到很多问题问这个问题,并试图用它们来帮助自己,但没有运气。
我的密码
Program TeamWrite;
Var FName, txt : String[10];
UserFile : Text;
BEGIN
FName := 'Team';
Assign(UserFile, 'C:\Team.dat');
Rewrite(UserFile);
Writeln('Enter players name and score separated by a space, type end to finish');
if txt = 'end' then;
BEGIN
Close(UserFile)
End;
Else
BEGIN
Readln(txt);
Writeln;
Writeln(UserFile,txt);
End;
Until(txt = 'end');
End.
最佳答案
在Pascal中,分号(即“;”)用于分隔语句,而不是结束它们。所以你应该说:
if txt = 'end' then
begin
Close(UserFile)
end
else
begin
Readln(txt);
Writeln;
Writeln(UserFile, txt)
end
请注意,在
then
之后,在else
之前和之后以及在end
之前的两个语句之后没有分号。另请注意,您可以在语句和
end
之间放置分号,例如:begin
WriteLn;
WriteLn(txt); <-- this is allowed
end
但是编译器会将其解释为好像该分号后面有一个空语句:
begin
WriteLn;
WriteLn(txt);
(an empty statement here)
end
不过,这是无害的。
“until”也是错误,因为它是保留字。在Pascal中,有一个“repeat ... until”循环,例如:
i := 0;
repeat
WriteLn(i);
i := i + 1
until i > 10
就像C的“do ... while”循环一样,只是条件相反。在您的程序中,我认为您应该在`if之前加一个
repeat
:repeat
if txt = 'end then
...
else
...
until txt = 'end'
关于syntax-error - Pascal错误-预期 “;”,但发现其他情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960875/