如何将类型的值按顺序链接到整数?

Aeq*_*tas 1 ada

我想要一个程序从用户那里取一个数字(从1到12)并且它将返回那个月的简短形式,例如:如果你写1它将返回JAN等.

我有以下内容:

type Month_Type is (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC);
Run Code Online (Sandbox Code Playgroud)

但是如何将其链接到userMonth : Integer;.我想像Month_Type(userMonth),但这不起作用,并给了我错误.我能想到的唯一另一件事是为每个Month_Type设置一个循环,并在那里有一个计数器,以便它匹配.但这看起来很乱,效率不高,必须有更好的方法.

Jac*_*sen 7

在Ada中,您可以更轻松地完成这项工作:

with Ada.Text_IO;
procedure Demo is
   type Month_Type is (Jan, Feb, Mar, [...], Dec);
   package Month_Text_IO is new Ada.Text_IO.Enumeration_IO (Month_Type);
   Input : Month_Type;
begin
   Month_Text_IO.Get (Input);
end Demo;
Run Code Online (Sandbox Code Playgroud)

无需处理两者之间的整数值.