Ada:如何检查输入是否为枚举类型 [英] Ada: How to check if input is an enumeration type
问题描述
我正在从键盘上读取输入。输入应该与枚举类型中定义的元素之一匹配。这是枚举类型的示例:
I am reading input from the keyboard. The input is supposed to match one of the elements defined in an enumeration type. Here is an example of the enum type:
type NameType is (Bob, Jamie, Steve);
如果我收到的输入不是这3个中的一个,则ada引发IO异常。我如何处理此问题,使其仅显示重试消息而又不让程序停止?谢谢
If I receive an input that is not one of these 3, ada raises an IO exception. How do I handle this to where I can simply display a "try again" message and not have the program stop? THANKS
推荐答案
创建 Enumeration_IO
for Name_Type
,例如说 Name_IO
。在 循环
,输入一个嵌套的 block 处理出现的任何 Data_Error
。当 Name_IO.Get
成功时, 退出
循环
。
Create an instance of Enumeration_IO
for Name_Type
, say Name_IO
. In a loop
, enter a nested block to handle any Data_Error
that arises. When Name_IO.Get
succeeds, exit
the loop
.
with Ada.IO_Exceptions;
with Ada.Text_IO;
procedure Ask is
type Name_Type is (Bob, Jamie, Steve);
package Name_IO is new Ada.Text_IO.Enumeration_IO (Name_Type);
begin
loop
declare
Name : Name_Type;
begin
Ada.Text_IO.Put("Enter a name: ");
Name_IO.Get(Name);
exit;
exception
when Ada.IO_Exceptions.Data_Error =>
Ada.Text_IO.Put_Line("Unrecognized name; try again.");
end;
end loop;
end Ask;
这篇关于Ada:如何检查输入是否为枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!