带字符串的Ada case语句

Ner*_*ool 3 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;
Run Code Online (Sandbox Code Playgroud)

注释部分正在完成我想要的,但使用if语句.我只是想看一下案例陈述是否可行.

Jac*_*sen 7

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

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;
Run Code Online (Sandbox Code Playgroud)