好像在Pascal語言里有這樣一個語法:
        b:= if( a>1,2,3)
就像Excel里寫公式一樣,但是在Delphi里就是行不通,請間該怎么寫?
我只想要一句最簡單的代碼實現,不想要比如:
    if a>1 then b:=2 eles b:=3;
也許沒有說清楚:(

解决方案 »

  1.   

    pascal 有这种写法?
    不过你完全可以自己写一个这样的函数。
    b:= if( a>1,2,3)c里面有一个类似的b= a>1 ? 2: 3;
      

  2.   

    同意pazee(耙子)说的,你可以自己写一个
      

  3.   

    简单的写法function iif (a: boolean; b, c: integer):integer;
    begin
      if a then
        result:= b
      else
        result:= c;
    end;
    。。
    b:= iif( a>1,2,3);
      

  4.   

    function myif(a: Boolean; b, c: Integer):Integer;
    asm
      test EAX,EAX
      jnz @True
      mov EAX,ECX
      ret
      @True:
      mov EAX,EDX
      ret
    end;
      

  5.   

    看到了吗?ifthen就可以,具体的看帮助。
    Conditionally returns one of two specified values.UnitMathStrUtils or Categorymiscellaneous routinesDelphi syntax:function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload
    ;function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64; overload;
    function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload;
    function IfThen(AValue: Boolean; const ATrue: string; const AFalse: string = ''): string; overload;C++ syntax:extern PACKAGE int __fastcall IfThen(bool AValue, const int ATrue const int AFalse);
    extern PACKAGE __int64 __fastcall IfThen(bool AValue, const __int64 ATrue const __int64 AFalse);
    extern PACKAGE double __fastcall IfThen(bool AValue, const double ATrue const double AFalse);
    extern PACKAGE AnsiString __fastcall IfThen(bool AValue, const AnsiString ATrue const AnsiString AFalse);DescriptionIfThen checks the expression passed as AValue and returns ATrue if it evaluates to true, or AFalse if it evaluates to false. In Delphi, if the AFalse parameter is omitted, IfThen returns 0 or an empty string when AValue evaluates to False.
      

  6.   

    其实我们很多自己写的函数,delphi都有,只不过我们没有去挖掘而已,大家一定要善于挖掘开发工具的潜力,充分利用开发工具和开发语言。
      

  7.   

    还有很多,
    比如:
      IsLeapYear,判断是否是闰年;
      DaysBetween,求两个日期相隔天数;
      DaysInMonth,
      DaysInAMonth,
      DaysInYear,
      ....
      还有数学运算函数
      ...
      等等,好多好多,大家一起来。
      

  8.   

    代码是经过批处理才开始执行的,即使看似简单的写法执行的时间还是和一个ifthen语句时间一样的
      

  9.   

    delphi里没有三元运算的!
    b = a > 1 ? 1:3 这是c里才有的
    delphi 里老老实实的:
    if a > 1 then b := 1
    else b := 3
    ok!
      

  10.   

    Delphi自带了这类函数的帮助,把光标放在上面按F1
      

  11.   

    Rainsea(飞龙在天) 厉害,能弄出这样的函数。
    很多这样函数平时很多人都会忽视的。即使要用的时候,也不知道怎么找出这样的函数。