【发布时间】:2018-06-14 10:55:10
【问题描述】:
所以下面是我创建的代码模块,用于验证用户输入是否在一个范围之间,但是这个错误一直出现在它的下方“预期的语句但找到了函数”,有什么想法吗?非常感谢
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;
【问题讨论】:
-
你要告诉 use 哪一行导致错误?
-
@Mawg,System.Write 可以处理多种类型的参数:
Each output expression must be of a type Char, one of the Integer types (Byte, Shortint, Word, Longint, Cardinal), one of the floating-point types (Single, Real, Double, Extended, Currency), one of the string types (PChar, AnsiString, ShortString), a packed string, or one of the Boolean types (Boolean, Bool). -
@Mawg:
Write和Writeln来自非常古老的时代。他们不需要像IntToStr或类似的转换例程之类的东西。两者都是“编译器魔术”例程,即编译器知道它们并根据参数生成不同的代码序列。这也使他们接受多个不同的参数,这在引入array of const之前是不可能的。 FWIW,如今,这些函数被某些人称为“内在函数”。 -
哈哈!几十年来,我可能一直在用 Write and()` 不必要地调用
IntToStr():-) -
@Mawg:您可能想阅读这些例程:Write、Writeln、Read、Readln 并看到其中一些甚至具有额外的格式化选项,例如
Write(I:8, J:8);which 格式化 I 和 J 正确在他们自己的“字段”中,每 8 个字符。对于应该快速(并且可读)输出简单测试结果的控制台程序非常有用。