RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 607955
Accepted
I_CaR
I_CaR
Asked:2020-12-26 17:37:20 +0000 UTC2020-12-26 17:37:20 +0000 UTC 2020-12-26 17:37:20 +0000 UTC

Case语句——如何实现第二条语句的触发。至少赋值运算符?

  • 772

假设有一个普通的操作员case

case a of
  1 : ShowMessage('a=1');
  2 : ShowMessage('a=2');
  3 : ShowMessage('a=3');
  4 : ShowMessage('a=4');
else ShowMessage('no');
end;

是否有可能实现这样的事情:

case a of
  1 : ShowMessage('a=1'); b:=10; 
  2 : ShowMessage('a=2'); b:=20;
  3 : ShowMessage('a=3'); b:=30;
  4 : ShowMessage('a=4'); b:=40;
else ShowMessage('no');
end;

这里有类似的东西,因为满足条件后a:=1;,需要给b变量赋值。


PS 或者你怎么能在不转换case为的情况下解决这里的情况if?

delphi
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Sublihim
    2020-12-26T17:40:42Z2020-12-26T17:40:42Z
    case a of
      1 : 
          begin
              ShowMessage('a=1'); 
              b:=10; 
          end;
      2 : 
          begin
              ShowMessage('a=2'); 
              b:=20;
          end;
      3 : 
          begin
             ShowMessage('a=3'); 
             b:=30;
          end;
      4 : 
          begin
             ShowMessage('a=4'); 
             b:=40;
          end;
      else ShowMessage('no');
    end;
    
    • 4
  2. Anton Shchyrov
    2020-12-26T19:50:35Z2020-12-26T19:50:35Z

    对于上面的代码,根本就case不需要

    if a in [1..4] then begin
      ShowMessageFmt('a=%d', [a]);
      b := a * 10;
    end else
      ShowMessage('no');
    
    • 4
  3. Kromster
    2020-12-26T20:43:31Z2020-12-26T20:43:31Z

    另一个可能的(并且更短,如果有很多对)选项,如果你有一个声明的类型或值范围a,是使用一个常量数组:

    type
      TPresetType = record Text: string; Value: Integer; end;
    const
      PRESETS: array [1..4] of TPresetType = (
        (Text: '=10'; Value: 10),
        (Text: '=20'; Value: 20),
        (Text: '=30'; Value: 30),
        (Text: '=40'; Value: 40)
      );
    begin
      if a >= Low(PRESETS) and a <= High(PRESETS) then
      begin
        ShowMessage(PRESETS[a].Text);
        b := PRESETS[a].Value; 
      end else
        ShowMessage('no');
    end;
    

    或几乎相同,但没有记录类型,有 2 个常量数组:

    const
      PRESET_TEXT: array [1..4] of string = ('=10', '=20', '=30', '=40');
      PRESET_VALUE: array [1..4] of Integer = (10, 20, 30, 40);
    begin
      if a >= Low(PRESET_TEXT) and a <= High(PRESET_TEXT) then 
      begin
        ShowMessage(PRESET_TEXT[a]);
        b := PRESET_VALUE[a]; 
      end else
        ShowMessage('no');
    end;
    

    这种解决方案的优点是什么——减少犯错的可能性:

    • 在 1 处列出所有选项和值
    • 如果您可以隔离变量的类型a(例如,a: TTextAlign;让我们写PRESET_TEXT: array [a] of string),那么在添加新值时,编译器将强制您完成所有值。
    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5