【问题标题】:Pascal error - ";" expected but Else found帕斯卡错误 - “;”预期但其他人发现
【发布时间】:2015-06-21 03:01:56
【问题描述】:

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

【问题讨论】:

    标签: syntax-error pascal


    【解决方案1】:

    在 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
    

    不过,这是无害的。

    “直到”也是一个错误,因为它是一个保留字。在 Pascal 中有一个“重复...直到”循环,例如:

    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'
    

    【讨论】:

    • 感谢您的快速响应,我已经尝试过了,但现在不是 Else 发现的错误,而是现在直到找到。
    • 那是因为“直到”是一个保留字。请看我上面的编辑。
    • 我仔细查看了您的程序,现在我知道您没有调用名为“Until”的函数。请再次查看我的编辑。
    【解决方案2】:

    我不熟悉 Pascal,但快速浏览一些网站,我认为您不需要 ;在第一个 if 语句之后:

    if txt = 'end' then;  
    

    应该是

    if txt = 'end' then
    

    【讨论】:

      【解决方案3】:

      你放错了一些;

      if txt = 'end' then; // remove ';' 
          BEGIN  
              Close(UserFile) //add `;`
          End;  // remove ';' 
      Else  
          BEGIN  
              Readln(txt);  
              Writeln;  
              Writeln(UserFile,txt);  
          End;  
      Until(txt = 'end'); 
      

      请参考here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多