string - 带字符串的 Ada case 语句

标签 string ada case-statement

我试图在 case 语句中使用字符串,但是它给了我 expected a discrete type. Found type Standard.String我知道字符串不是离散的。我想知道是否有解决方法。这是我的代码:

function Is_Valid_Direction(Direction_To_Go : in String) return Integer is
  Room : Integer := 0;
   begin
      --if (Direction_To_Go = "NORTH" or Direction_To_Go = "N") then
      --   Room := Building(currentRoom).exits(NORTH);
      --elsif (Direction_To_Go = "SOUTH" or Direction_To_Go = "S") then
      --   Room := Building(currentRoom).exits(SOUTH);
      --elsif (Direction_To_Go = "EAST" or Direction_To_Go = "E") then
      --   Room := Building(currentRoom).exits(EAST);
      --elsif (Direction_To_Go = "WEST" or Direction_To_Go = "W") then
      --   Room := Building(currentRoom).exits(WEST);
      --elsif (Direction_To_Go = "UP" or Direction_To_Go = "U") then
      --   Room := Building(currentRoom).exits(UP);
      --elsif (Direction_To_Go = "DOWN" or Direction_To_Go = "D") then
      --   Room := Building(currentRoom).exits(DOWN);
      --end if;
      case Direction_To_Go is
         when "NORTH" | "N" => Room := Building(currentRoom).exits(NORTH);
         when "SOUTH" | "S" => Room := Building(currentRoom).exits(SOUTH);
         when "EAST" | "E" => Room := Building(currentRoom).exits(EAST);
         when "WEST" | "W" => Room := Building(currentRoom).exits(WEST);
         when "UP" | "U" => Room := Building(currentRoom).exits(UP);
         when "DOWN" | "D" => Room := Building(currentRoom).exits(DOWN);
         when others => Room := 0;
      end case;
      return Room;
   end Is_Valid_Direction;

评论部分正是我想要的,但有 if 语句。我只是想看看是否可以使用 case 语句。

最佳答案

您可以将字符串映射到离散类型。最简单的是枚举类型:

procedure Light (Colour : in     String) is
   type Colours is (Red, Green, Blue);
begin
   case Colours'Value (Colour) is -- ' <- magic ;-)
      when Red =>
         Switch_Red_LED;
      when Green =>
         Switch_Green_LED;
      when Blue =>
         Switch_Blue_LED;
   end case;
exception
   when Constraint_Error =>
      raise Constraint_Error with "There is no " & Colour & " LED.";
end Light;

关于string - 带字符串的 Ada case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29505920/

相关文章:

java - 将十六进制字符串转换为字节数组

java - 处理文本文件中的字符串并继续执行 java 程序

c - 如果条件发生故障,内部进行字符串比较

c - 在 C 解决方案段错误中反转字符串

ada - 用一个字段创建 Ada 记录

linux - 忽略多个可能值的 Case 语句

Haskell - 也许算术

types - 将类型转换为字符串

compiler-errors - 在 ada 中使用静态库中的包

hadoop - Impala 查询获取下一个日期