尝试并捕捉帕斯卡
我正在使用 Dev-Pas 1.9.2,并试图确保输入符号或字母值时程序不会崩溃。
我用谷歌搜索了又搜索,但找不到任何关于如何实现这一目标的资源。
非常感谢任何帮助。谢谢!
这是我尝试管理输入的代码:
Function GetMenuChoice : Integer;
Var
OptionChosen : Integer;
Begin
Write('Please enter your choice: ');
Readln(OptionChosen);
If (OptionChosen < 1) Or ((OptionChosen > 4) And (OptionChosen <> 9))
Then
Begin
Writeln;
Writeln('That was not one of the allowed options. Please try again: ');
End;
GetMenuChoice := OptionChosen;
End;
I'm using Dev-Pas 1.9.2 and am trying to make sure the program doesn't crash when a symbol or a letter value is entered.
I've googled and googled and can't find any resoruce on how to achieve this.
Any help is greatly appreciated. Thanks!
Here is the code I'm trying to manage the input:
Function GetMenuChoice : Integer;
Var
OptionChosen : Integer;
Begin
Write('Please enter your choice: ');
Readln(OptionChosen);
If (OptionChosen < 1) Or ((OptionChosen > 4) And (OptionChosen <> 9))
Then
Begin
Writeln;
Writeln('That was not one of the allowed options. Please try again: ');
End;
GetMenuChoice := OptionChosen;
End;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改您的代码以接受 Char ;如果出于某种原因需要整数,请稍后处理转换。
这在德尔福中有效;除非您不能使用像
['1'..'4','9']
这样的集合和集合运算符,否则它应该可以正常工作。如果您绝对需要返回数字,请将返回类型更改回整数(或字节),然后将最后一行更改为:
或
Change your code to accept a Char instead; if you need an integer for some reason, handle the conversion afterward.
This works in Delphi; unless you can't use sets like
['1'..'4','9']
and set operators, it should work fine.If you absolutely need a number to be returned, change the return type back to integer (or byte) and then change the final line to:
or
(除了 Ken White 的)
我认为可以对 char 类型变量使用 READ,并保存用户输入 Enter。
但我会选择字符串类型版本并使用 VAL。
对于后者,还有其他方法(例如使用单元 Crt 或 Video),但这可能超出了作业的范围
(in addition to Ken White's)
I think one can use READ for a char typed variable, and save the user to type enter.
But I would go for a string typed version and use VAL.
For the latter there are other methods (e.g. using unit Crt or Video), but that probably goes beyond the scope of the assignment
您真的想接受四种不同的可能输入吗?
(数字 1、2、3、4 和 9)
这就是你现在所要求的。
注意:即使像第一个回答者建议的那样进行了更改,您的代码也有一个主要的
问题。如果给出 5 或 Q 会发生什么……你抱怨,然后退出
例行公事。
在原始代码中,如果我输入 100,您将打印“这是不允许的”...
然后返回 100 给调用者。
提示:循环。
提示 2:确保您不会永远循环
顺便说一句,从不这样做:
ord(某些字符)- 48
相反,总是使用:
ord(某些字符)-ord('0')
为什么?两个明显的原因:
可读性。 48 是多少?
正确性。如果您是在非 ASCII 系统上编译的,则 48 可能不是
0 的字符代码。
0. Stan
Do you really want to accept exactly four different possible inputs?
(The numbers 1, 2, 3, 4, and 9)
That's what you're asking for at the moment.
Note: even with a change like the first answerer suggested, your code has a major
problem. What happens if a 5 or Q is given ... you complain, AND THEN EXIT
THE ROUTINE.
In the original code, if I enter a 100, you'll print the "That was not allowed"...
and then return 100 to the caller.
Hint: loop.
Hint 2: ensure you don't loop forever
BTW, NEVER do:
ord (some character) - 48
instead, always use:
ord (some character) - ord ('0')
Why? Two obvious reasons:
readability. What's 48?
correctness. If you're compiled on a non-ASCII system, 48 may not be
the character code for 0.
Stan