如何在 Pascal 中检查数组中的所有元素

How to check all elements in an array in pascal

我该如何编写代码,以便如果某个元素等于某个值,它会显示一条消息,但如果该数组内的所有元素都不等于该值,那么它会输出 'None'?

我试过了

for i := 0 to high(array) do
begin
    if (array[i].arrayElement = value) then
    begin
        WriteLn('A message');
    end;
end;

那个位有效,但我不知道如何进行检查。我有这个:

if (array[i].arrayElement to array[high(array)].arrayElement <> value) then
begin
    WriteLn('None');
end;

但它不允许我使用 "to"

这个写个辅助函数最清楚:

function ArrayContains(const arr: array of Integer; const value: Integer): Boolean;
var
  i: Integer;
begin
  for i := Low(arr) to High(arr) do
    if arr[i] = value then
    begin
      Result := True;
      Exit;
    end;
  Result := False;
end;

或使用for/in:

function ArrayContains(const arr: array of Integer; const value: Integer): Boolean;
var
  item: Integer;
begin
  for item in arr do
    if item = value then
    begin
      Result := True;
      Exit;
    end;
  Result := False;
end;

然后你这样称呼它:

if not ArrayContains(myArray, myValue) then
  Writeln('value not found');