syntax-error - Pascal错误-预期 “;”,但发现其他情况

标签 syntax-error pascal

我正在使用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/

相关文章:

interpreter - Free Pascal 有命令行解释器吗?

pascal - 是否可以仅打开一次文件来读取和写入文件?

c - 我如何在 Pascal 中用 C 编写这一行?

delphi - 从文本文件中读取特定行

javascript - node.js 语法错误 : Invalid or unexpected token

mysql - 这个mysql语法有什么问题?第一个主键出错

bash - 不可能的 bash : syntax error: invalid arithmetic operator (error token is "")

c - C 中的线程问题

python - Pascal/Python 分解程序

mysql - codeigniter 生成的更新中的语法错误