在Delphi中,我如何找到最大的变量(整数)

时间:2017-11-13 17:07:33

标签: integer compare delphi-10.2-tokyo

我有四个名为C,S,F和U的整数变量,每个变量根据文件例程的结果得到赋值,它们代表 C S 成功, F ailed和 U serDefined。找出哪个变量保持最大值的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

var
  C, S, F, U: Integer;

  function WhichIsHighest(const Values: array of Integer): Integer;
  var
    I, Highest: Integer;
  begin
    Result := Low(Values);
    Highest := Values[Result];
    for I := Result+1 to High(Values) do begin
      if Values[I] > Highest then begin
        Result := I;
        Highest := Values[I];
      end;
    end;

begin
  ... set C, S, F, U as needed ... 

  case WhichIsHighest([C, S, F, U]) of
    0: ...; // C is highest
    1: ...; // S is highest
    2: ...; // F is highest
    3: ...; // U is highest
  end;
end;