我将如何编码,以便如果一个元素等于某个值,它会显示一条消息,但如果该数组中的所有元素都不等于该值,那么它将输出"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');