如何将字符串与整数与pascal中的循环进行比较



如何在pascal中使用循环时将字符串与整数进行比较?
这样:

var Destination:string;
while (Destination>'11') do begin
    writeln('Error');
    write('Destination Number');
    readln(Destination);
end;

您必须将Destination转换为整数:

program Project1;
uses sysutils;
var
  converted: integer;
  Destination: string;
begin
  converted := 12;
  Destination := '';
  while (converted > 11) do
  begin
    writeln('Error');
    writeln('Destination Number');
    readln(Destination);
    converted := StrToIntDef(Destination, 12);
  end;
end.

转化例程在系统中可用:

http://www.freepascal.org/docs-html/rtl/sysutils/index-5.html

为什么不只是在while-do语句中进行转换?

ReadLn(Destination);
WHILE StrToInt(Destination) > 11 DO NumberIsTooHigh;

numberistoo -high只是您编写以处理"错误"的过程。例如:

PROCEDURE NumberIsTooHigh;
  BEGIN
    WriteLn('Your number is above valid range');
    write('Destination Number');
    readln(Destination);
  END;

首次运行上以前的例程"错误"的原因是"目标"尚未具有值。然后,将转换的变量手动设置为12,就在OK-range之外,因此它将始终在启动时产生错误。

最新更新