gpt4 book ai didi

delphi - 如何对案例进行多种变体

转载 作者:行者123 更新时间:2023-12-04 01:17:08 25 4
gpt4 key购买 nike

我正在使用 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 Statements实际工作。

请注意,在原始代码中,if (number> 8) and (number< 10) thenif (number = 9) then相同,并且您正在跳过 message 的分配如果number是 10、17 还是 18,这是您真正想要的吗?

关于delphi - 如何对案例进行多种变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63160344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com