gpt4 book ai didi

delphi - 把所有这些 if 语句写得更干净

转载 作者:行者123 更新时间:2023-12-03 15:38:03 25 4
gpt4 key购买 nike

我有一个问题,当我向 if 语句添加更多“名称”时。我很难看出那里是否一切准备就绪。因此,有没有一种更简洁的方式来编写此内容,我可以轻松地看到那里都读取了哪些名称?

    function TfDB.GetW(name: string) :integer;
begin
result := 0;
if (name = 'Destacker') or (name='Router') or (name = 'Turn Table') Then
result := 57;
if (name = 'Laser Marker') then
result := 66;
if (name = 'SP28')OR(name='Chamber') OR (name = 'CM402') OR (name = 'SP60') then
result := 65;
if (name = 'Conveyor') OR (name = 'Slide Gate') OR (name = 'Washer') then
result := 51;
if (name = 'BTU') OR (name = 'Furukawa') OR (name = 'Radial') OR (name = 'HDP') or (name = 'MSR') OR (name = 'Koki') Or (name = 'MSF') then
result := 98;
if (name = 'Buffer')OR (name = 'Reclaimer') OR (name = 'ECO Roller') then
result := 49;
if (name = 'Inverter') or (name = 'CNC') then
result := 42;
if (name = '3-D Check Station') or (name='Screw Machine') or (name='VT-Win') or(name='Component Viewer') then
result := 58;
if (name = 'AOI Panel') or (name='AirBlow') then
result := 42;
if (name='Mag Loader') or (name='Soltec') then
result := 73;
if (name='Tester') then
result := 33;
if (name='LoadBox') then
result := 17;
if (name = 'DeltaWave') then
result := 89;
if (name = 'ScrewFeeder') then
result := 25;
if (name='Pump') then
result := 33;

//if result is 0 show message error.

end;

最佳答案

您可以创建一个字典,TDictionary<string, Integer> ,并将其存储在全局变量中。在初始化时用名称到宽度映射加载它。然后你的函数就变成了一个单行函数。

var
WidthMapping: TDictionary<string, Integer>;
....
function TfDB.GetW(name: string) :integer;
begin
if not WidthMapping.TryGetValue(name, Result) then
... handle error condition
end;
....
initialization
WidthMapping := TDictionary<string, Integer>.Create;
WidthMapping.Add('Destacker', 57);
WidthMapping.Add('Router', 57);
... etc.
....
finalization
WidthMapping.Free;

关于delphi - 把所有这些 if 语句写得更干净,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14723890/

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