8

下面的 if 语句可以转换为 case 语句吗?

if (Number >= 5) and (Number <= 10) then
  lblAnswer.Caption := 'in range'
else
  lblAnswer.Caption := 'out of range';

我的答案 :

是的,它可以

case (number >= 5) and (Number <= 10) of
  lblAnswer.Caption := 'in range';
  lblAnswer.Caption := 'out of range';
end;

这个对吗?

4

3 回答 3

26

如果 Number 具有某种整数数据类型,则:

case number of
5 .. 10: lblAnswer.Caption := 'in range';
else     lblAnswer.Caption := 'out of range';
end;
于 2011-10-18T05:50:35.967 回答
8

一个小修正:

case (number >= 5) and (Number <= 10) of
  true:lblAnswer.Caption := 'in range';
  false:lblAnswer.Caption := 'out of range';
end;
于 2011-10-18T05:59:15.427 回答
1
Function InRange (Lo,Hi,Val : Integer) : Boolean;
Begin
 Result := (Val>=Lo)And(Val<=Hi);
End;
于 2014-04-10T12:47:43.423 回答