我正在使用 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;
更改您的代码以接受 Char ;如果出于某种原因需要整数,请稍后处理转换。
这在德尔福中有效;除非你不能使用像这样的集合['1'..'4','9']
和设置运算符,它应该可以正常工作。
Function GetMenuChoice : Char;
Var
OptionChosen : Char;
Begin
repeat
Write('Please enter your choice: ');
Readln(OptionChosen);
If not (OptionChosen in ['1'..'4', '9'])
Then
Begin
Writeln;
Writeln('That was not one of the allowed options. Please try again: ');
End;
until OptionChosen in ['1'..'4', '9'];
GetMenuChoice := OptionChosen;
End;
如果您绝对需要返回数字,请将返回类型更改回整数(或字节),然后将最后一行更改为:
GetMenuChoice := Ord(OptionChosen) - 48;
or
GetMenuChoice := Ord(OptionChosen) - Ord('0');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)