长度检查帕斯卡
我在第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,你需要写
括号是必需的。
其次,这总是评估为
false
,因为不存在既为空又长度为11或更长的字符串。事实上,一个字符串当且仅当其长度为零时才是空的,所以基本上你会说“如果长度为零且长度为 11 或更大,那么......”。您很可能希望使用析取,即使用
or
而不是and
:如果名称为空或<,这将显示错误消息/em> 如果太长。
此外,即使名称无效,循环也会退出,因为如果
PlayerName
等于ThisIsATooLongName
那么确实PlayerName <> ''
。你需要的是类似
或
First, you need to write
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 ofand
: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 toThisIsATooLongName
then indeedPlayerName <> ''
.What you need is something like
or
嗯,我遇到了类似的情况,
想知道这是否可行,我放置的程序无法编译。
Urm Im in a similar situation,
Wondering if this would work, the program I put this is wont compile.