簡體   English   中英

如何對案例進行多種變體

[英]How to make multiple variations on case

我正在使用 delphi 運行以下代碼:

if (number> 8) and (number< 10) then
    message:= 'first option'
else if (number> 11) and (number< 17) then
    message:= 'second option'
else if (number> 18) then
    message:= 'third option';

我需要做確切的代碼但使用case ,我正在嘗試但沒有找到任何解釋如何做的內容:

case idade of
(case > 8 and case< 10) : message:= 'first option';
(case > 11 and case< 17) : message:= 'second option';
(case > 18) : message:= 'third option';
end;

我也嘗試搜索有關案例的問題,但我想我也沒有找到找到此答案的正確方法。

使用case語句可以得到的最接近結果如下所示:

case idade of
  9: message := 'first option';
  12..16: message := 'second option';
else
  if idade > 18 then
    message := 'third option';
end;

或者這個(感謝@AndreasRejbrand):

case idade of
  9: message := 'first option';
  12..16: message := 'second option';
  19..MaxInt{idade.MaxValue}: message := 'third option';
end;

您可能想閱讀 Embarcadero 的文檔,了解Case 語句的實際工作原理。

請注意,在原始代碼中, if (number> 8) and (number< 10) thenif (number = 9) then相同,如果numbermessage或18、那是你真正想要的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM