比较输入的字符串和枚举类型 [英] Compare a string input to an Enumerated type

查看:323
本文介绍了比较输入的字符串和枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个字符串与和枚举的比较。我已经写了我要尝试的示例代码。由于String和枚举类型不同,因此如何在Ada中正确执行此操作?

I am looking to do a comparison of a string to and enumeration. I have written a sample code of what I am attempting. Since a String and and Enumerated type are different, how do I go about doing this properly in Ada?

WITH Ada.Text_IO; USE Ada.Text_IO;

PROCEDURE ColorTest IS

   TYPE StopLightColor IS (red, yellow, green);

   response : String (1 .. 10);
   N : Integer;

BEGIN
   Put("What color do you see on the stoplight? ");
   Get_Line (response, N);
   IF response IN StopLightColor THEN
      Put_Line ("The stoplight is " & response(1..n));
   END IF;

END ColorTest;


推荐答案

首先实例化 Enumeration_IO StopLightColor

package Color_IO is new Ada.Text_IO.Enumeration_IO(StopLightColor);

然后您可以执行以下任一操作:

Then you can do either of the following:


  • 使用 Color_IO.Get 读取值,捕获任何 Data_Error 出现,如此处所示,用于 Enumeration_IO 的类似实例。

  • Use Color_IO.Get to read the value, catching any Data_Error that arises, as shown here for a similar instance of Enumeration_IO.

使用 Color_IO.Put 获得 String 响应进行比较。

另外,对于枚举类型的标识符, Stoplight_Color 可能是更一致的样式。

As an aside, Stoplight_Color might be a more consistent style for the enumerated type's identifier.

这篇关于比较输入的字符串和枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆