Ada:获取整数时如何管理 Data_error 异常
我试图让用户从菜单中选择一种模式,例如
- Eat
- Drink
- Sleep
现在我可以通过使用调用获得整数输入
ada.integer_text_io.get(integer_variable);
这里的主要问题是,如果我插入非数字字符串(例如字符串)引发以下异常
raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tiinio.adb:89 instantiated at a-inteio.ads:18
我尝试接收一个字符,检查它是否是整数,然后将其转换为整数,但后来我意识到我需要获取大于 1 位数字的整数的输入,因此字符方法不会'不工作。
如果我收到一个字符串,那么我无法检查它是否是整数(除非我扫描整个字符串以查看其所有字符是否都是整数...)
除了扫描整个字符串之外还有其他解决方案吗细绳? 或者也许是一种异常处理技术,可以防止程序终止并再次要求正确的整数?
-CH
I'm trying to make the user select a mode from the menu for example
- Eat
- Drink
- Sleep
Now I can get an integer input by using the call
ada.integer_text_io.get(integer_variable);
The main problem here is that if I insert a non digit string(such as a character string) the following exception is raised
raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tiinio.adb:89 instantiated at a-inteio.ads:18
I've tried receiving a character, check if it is a integer, then convert it into a integer, but then I realized that I need to get inputs for integer larger than 1 digits, so the character method won't work.
If I receive a string, then I can't check to see if it is a integer or not(unless i scan through the whole string to see if all its characters are integer...)
Is there another solution other than scanning the whole string?
Or perhaps a exception handling technique that might keep the program from terminating and ask again for a proper integer?
-CH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当/如果读取无效字符串时,几乎所有在 Ada 中将字符串解析为某种标量值的标准方法都会产生某种异常。这并没有什么问题。只需处理异常即可。
即使您将自己的字符串解析编写为整数例程,您也必须以某种方式处理用户输入无效字符串的情况。正确的?
我想涉及的唯一“技术”是您可以将异常处理程序放在子例程上,甚至放在代码中内联的
declare ... begin ... end
块上。这样,只有块内的代码会被中止。一般来说,我更喜欢看到使用子例程。所以你会得到类似这样的信息:现在,既然如此,对于快速而肮脏的 Ada 菜单,我通常不会像上面那样使用数字菜单。相反,创建一个枚举类型。这样您就可以在菜单类型循环中使用
'image
打印菜单选项,当您使用'value
或时,Ada 将处理文本解析Ada.Text_IO.Enumeration_IO
。这样做的好处是,当菜单选项列表发生变化时,您不必更改菜单打印代码或解析代码。
Pretty much all the standard ways to parse a string into a scalar value of some kind in Ada will produce an exception of some kind when/if an invalid string is read. There isn't anything wrong with that. Just handle the exception.
Even if you wrote your own string parsing to integer routines, you'd have to somehow handle the situation where the user entered an invalid string. Right?
I guess the only "technique" involved is that you can put exception handlers on subroutines, or even on
declare ... begin ... end
blocks that you put inline in your code. This way only the code within the block is aborted. Generally I prefer to see subroutines used. So you'd get something like:Now, this being the case, for quick-and-dirty Ada menus I generally don't do numeric menus like above. Instead, make an enumerated type. That way you can print the menu options using a
'image
in a loop through the menu type, and Ada will handle the text parsing when you use'value
orAda.Text_IO.Enumeration_IO
.The nice thing about this is that you don't have to change your menu-printing code or your parsing code when the list of menu options changes.
用于请求此类用户输入的主要 Ada 编程习惯使用 Get_Line:
或
现在用户的响应位于字符串中,您可以快速扫描非数字字符,或使用 Integer'Value 属性将其转换为整数(将调用包装在合适的异常处理程序中)。例如:
这个习惯用法的额外优点是您也可以接受非数字输入,例如“Q”表示退出,或“退出”;并且还进行可能需要的任何字符预处理,例如大小写。
The predominant Ada programming idiom for requesting user input of this sort uses Get_Line:
or
Now that the user's response is in a string you can do a quick scan for non-numeric characters, or use the Integer'Value attribute to convert it to an integer (wrapping the invocation in a suitable exception handler). For example:
The added advantage of this idiom is that you can accept non-numeric input as well, such as 'Q' for Quit, or "Quit"; and also do any character preprocessing, like up-casing, that might be needed.
您可以处理异常,要么在循环中重复请求输入,直到输入整数,要么在结束时优雅地退出......
You can handle the exception, either within a loop to repeatedly ask for input until an integer is entered, or towards the end to exit gracefully...