首先,你需要写
If (PlayerName = '') And (Length(PlayerName) > 10) Then
括号是必需的。
其次,这将总是评估为false
,因为没有字符串都是空的,长度为11或更多。事实上,一个字符串是空的,当且仅当它的长度为零时,基本上你会说“如果长度为零,长度为11或更大,则...”。
最有可能你想,而不是使用脱节,也就是使用or
而不是and
:
If (PlayerName = '') Or (Length(PlayerName) > 10) Then
如果名称为空或这将显示错误消息,如果太长。
另外,即使名称无效,循环也会退出,因为如果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;
请不要编辑问题的方式,使已发布的答案不相关。现在你已经创建了一个新的bug。现在,程序不会退出,直到它太长时间才会停止询问名称......您应该按照下面的示例进行操作。 – 2011-05-21 13:27:08
对不起。我已经编辑回原来的代码。 – orange 2011-05-21 13:28:45