长度检查帕斯卡

发布于 2024-11-09 12:55:12 字数 431 浏览 5 评论 0原文

我在第 7 行收到错误错误:运算符未重载。我是否必须再次重复并且无法使用 and 运算符?

Function GetValidPlayerName : String;
  Var
    PlayerName : String;
  Begin
    Repeat
      Readln(PlayerName);
      If PlayerName = '' And Length(PlayerName) > 10
        Then Write('That was not a valid name.  Please try again: ');
    Until PlayerName <> '';
    GetValidPlayerName := PlayerName;
  End;

I get the error Error: Operator is not overloaded on line 7. Do I have to do a another repeat and can't use the and operator?

Function GetValidPlayerName : String;
  Var
    PlayerName : String;
  Begin
    Repeat
      Readln(PlayerName);
      If PlayerName = '' And Length(PlayerName) > 10
        Then Write('That was not a valid name.  Please try again: ');
    Until PlayerName <> '';
    GetValidPlayerName := PlayerName;
  End;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

记忆里有你的影子 2024-11-16 12:55:12

首先,你需要写

If (PlayerName = '') And (Length(PlayerName) > 10) Then

括号是必需的。

其次,这总是评估为false,因为不存在既为空又长度为11或更长的字符串。事实上,一个字符串当且仅当其长度为零时才是空的,所以基本上你会说“如果长度为零且长度为 11 或更大,那么......”。

您很可能希望使用析取,即使用 or 而不是 and

If (PlayerName = '') Or (Length(PlayerName) > 10) Then

如果名称为空或<,这将显示错误消息/em> 如果太长。

此外,即使名称无效,循环也会退出,因为如果 PlayerName 等于 ThisIsATooLongName 那么确实 PlayerName <> ''

你需要的是类似

Function GetValidPlayerName : String;
Var
  PlayerName : String;
Begin
  Repeat
    Readln(PlayerName);
    If (PlayerName = '') Or (Length(PlayerName) > 10) Then
    Begin
      Write('That was not a valid name.  Please try again: ');
      PlayerName := '';
    End;
  Until PlayerName <> '';
  GetValidPlayerName := PlayerName;
End;

Function GetValidPlayerName : String;
Var
  PlayerName : String;
Begin
  result := '';
  Repeat
    Readln(PlayerName);
    If (PlayerName = '') Or (Length(PlayerName) > 10) Then
      Write('That was not a valid name.  Please try again: ')
    Else
      result := PlayerName;
  Until result <> '';
End;

First, you need to write

If (PlayerName = '') And (Length(PlayerName) > 10) Then

The parentheses are required.

Secondly, this will always evaluate to false, because there is no string that is both empty and has length 11 or more. Indeed, a string is empty if and only if its length is zero, so basically you say "if the length is zero and the length is 11 or more, then...".

Most likely you wish instead to use a disjunction, that is, to use or instead of and:

If (PlayerName = '') Or (Length(PlayerName) > 10) Then

This will display the error message if the name is empty or if it is too long.

In addition, the loop will exit even if the name is invalid, because if PlayerName is equal to ThisIsATooLongName then indeed PlayerName <> ''.

What you need is something like

Function GetValidPlayerName : String;
Var
  PlayerName : String;
Begin
  Repeat
    Readln(PlayerName);
    If (PlayerName = '') Or (Length(PlayerName) > 10) Then
    Begin
      Write('That was not a valid name.  Please try again: ');
      PlayerName := '';
    End;
  Until PlayerName <> '';
  GetValidPlayerName := PlayerName;
End;

or

Function GetValidPlayerName : String;
Var
  PlayerName : String;
Begin
  result := '';
  Repeat
    Readln(PlayerName);
    If (PlayerName = '') Or (Length(PlayerName) > 10) Then
      Write('That was not a valid name.  Please try again: ')
    Else
      result := PlayerName;
  Until result <> '';
End;
再见回来 2024-11-16 12:55:12

嗯,我遇到了类似的情况,

while(Length(conversionrates[i].rate)<>2)) do
begin
    writeln('the conversion name should be 2 letters. (E.G Pounds to Dollars would be "PD")');
    readln(conversionrates[i].fromto);
end;

想知道这是否可行,我放置的程序无法编译。

Urm Im in a similar situation,

while(Length(conversionrates[i].rate)<>2)) do
begin
    writeln('the conversion name should be 2 letters. (E.G Pounds to Dollars would be "PD")');
    readln(conversionrates[i].fromto);
end;

Wondering if this would work, the program I put this is wont compile.

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