尝试并捕捉帕斯卡

发布于 2024-11-09 02:45:56 字数 569 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放血 2024-11-16 02:45:56

更改您的代码以接受 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;  

GetMenuChoice := Ord(OptionChosen) - Ord('0');

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.

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;

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:

GetMenuChoice := Ord(OptionChosen) - 48;  

or

GetMenuChoice := Ord(OptionChosen) - Ord('0');
一页 2024-11-16 02:45:56

(除了 Ken White 的)

  1. 我认为可以对 char 类型变量使用 READ,并保存用户输入 Enter。

  2. 但我会选择字符串类型版本并使用 VAL。

    • 它与编码无关,并且
    • 该原则超出了值 9。
    • 但需要输入,大量输入会弄乱您的屏幕。

对于后者,还有其他方法(例如使用单元 Crt 或 Video),但这可能超出了作业的范围

(in addition to Ken White's)

  1. I think one can use READ for a char typed variable, and save the user to type enter.

  2. But I would go for a string typed version and use VAL.

    • It is something more encoding agnostic, and
    • the principle extends beyond value 9.
    • requires an enter though, and heavy input will mess up your screen.

For the latter there are other methods (e.g. using unit Crt or Video), but that probably goes beyond the scope of the assignment

溺孤伤于心 2024-11-16 02:45:56

您真的想接受四种不同的可能输入吗?
(数字 1、2、3、4 和 9)
这就是你现在所要​​求的。

注意:即使像第一个回答者建议的那样进行了更改,您的代码也有一个主要的
问题。如果给出 5 或 Q 会发生什么……你抱怨,然后退出
例行公事。

在原始代码中,如果我输入 100,您将打印“这是不允许的”...
然后返回 100 给调用者。

提示:循环。

提示 2:确保您不会永远循环

顺便说一句,从不这样做:ord(某些字符)- 48
相反,总是使用:ord(某些字符)-ord('0')

为什么?两个明显的原因:

  1. 可读性。 48 是多少?

  2. 正确性。如果您是在非 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:

  1. readability. What's 48?

  2. correctness. If you're compiled on a non-ASCII system, 48 may not be
    the character code for 0.

Stan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文