代码之家  ›  专栏  ›  技术社区  ›  TopherCharles

应为语句,但函数在我的函数代码中发现错误

  •  0
  • TopherCharles  · 技术社区  · 7 年前

    下面是我为验证用户输入是否在一个范围内而创建的代码模块,但是这个错误一直出现在它下面“statement expected but function found”,有什么想法吗?多谢

    function get_choice(var Options: integer): integer;
          var
          choice: integer;
    
          begin
            while (true) do
              begin
                  write('choose option 1 to', Options);
    
                  try
                    readln(choice);
                    if (choice>=1) and (choice <=Options) then
                        get_choice := choice
                    else
                        write('invalid range');
                  except
                     write('not a number');
              end;
          end;
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   Andreas Rejbrand    7 年前

    你错过了一个 end :

                  try
                    readln(choice);
                    if (choice>=1) and (choice <=Options) then
                        get_choice := choice
                    else
                        write('invalid range');
                  except
                     write('not a number');
                  end;
             end
          end;
    

    尝试 try..except 块必须有自己的结束。

        2
  •  5
  •   H.Hasenack    7 年前

    正如沙拉姆所说,你错过了 end try..except..end 块。不过,我想我可以用一些小技巧来加强答案,以帮助你以后避免这种情况。

    • 保持你的( begin .. end 同一列上的块。同样 尝试..除了..结束 , try..finally..end repeat..until 挡住航道。
    • 利用IDE的选项将BEGIN和AND块与线连接起来。”结构突出显示。检查所附的截图。

    Structural highlighting in action

    Structural highlighting option