如何在Delphi中为if语句指定多个范围? 对于计数器:=1到lengthofpassword do 开始 currentletter:=密码[计数器]; currentascii:=Ord(currentletter); 如果(96
多亏了上面的一条评论,我找到了一个解决方案。我最后使用了这样一个案例:如何在Delphi中为if语句指定多个范围? 对于计数器:=1到lengthofpassword do 开始 currentletter:=密码[计数器]; currentascii:=Ord(currentletter); 如果(96,delphi,Delphi,多亏了上面的一条评论,我找到了一个解决方案。我最后使用了这样一个案例: for counter := 1 to lengthofpassword do begin currentletter:=password[counter]; currentascii:=Ord(currentletter); if (currentascii<48) AND (currentascii>57) then asciipoints:=asciipoints
for counter := 1 to lengthofpassword do
begin
currentletter:=password[counter];
currentascii:=Ord(currentletter);
if (currentascii<48) AND (currentascii>57) then
asciipoints:=asciipoints+1;
if (currentascii<65) AND (currentascii>90) then
asciipoints:=asciipoints+1;
if (currentascii<97) AND (currentascii>122) then
asciipoints:=asciipoints+1;
Writeln(asciipoints);
end;
if password[counter].IsLetterOrDigit then ...
再次感谢。多个范围最好用一个
案例来表达
语句:
for counter := 1 to lengthofpassword do
begin
currentletter:=password[counter];
currentascii:=Ord(currentletter);
case currentascii of
97..122 : asciicheck:=true;
65..90 : asciicheck:=true;
48..57 : asciicheck:=true;
else asciicheck:=false;
end;
end;
现在,这适用于<#128的字符。如果您在unicode应用程序中工作,并且不希望字符限制为英语字母,则可以使用
很高兴你自己找到了答案 另一种确保密码只包含大小写字符和数字的方法是我试图指出的:定义一组有效字符,并检查密码中的每个字符是否都是这些有效字符 对于这样定义的集合:
for counter := 1 to lengthofpassword do
begin
currentletter:=password[counter];
currentascii:=Ord(currentletter);
if (currentascii<48) AND (currentascii>57) then
asciipoints:=asciipoints+1;
if (currentascii<65) AND (currentascii>90) then
asciipoints:=asciipoints+1;
if (currentascii<97) AND (currentascii>122) then
asciipoints:=asciipoints+1;
Writeln(asciipoints);
end;
if password[counter].IsLetterOrDigit then ...
您可以使用如下语句
如果密码[I]在ValidChars中,则
但是,此语句将在Unicode Delphi中生成编译器警告,因为集合中的类型限制为256个可能的值,并且它们的顺序必须介于0和255之间。对于具有65.536个值的WideChar,情况并非如此。因此定义的字符集实际上是AnsiChar
的集。对于此任务,这是accep表中,因为需要检查的每个字符都是ASCII字符,所以如果密码包含Unicode字符,则使用该函数将不会生成编译器警告并具有定义的行为-返回False
这是生成的代码:
const
ValidChars = ['A'..'Z', 'a'..'z', '0'..'9'];
你课程的当前主题是什么?你需要如何解决这项任务?是否有任何要求?否则你可能会想研究字符集。为此,我最后使用了一个语句案例,使用集合,如97..122。对于Alphabethanks,这基本上就是我想到的。如果Ord(密码[计数器])在[48..57, 65..90, 97..122]然后
?对我来说似乎更简单。请注意,该集合将被编译为常量。@RudyVelthuis,如果您不知道,编译器在这两种情况下生成相同的代码。因此选择哪种解决方案更像是个人偏好。即使结果代码相同,像这样的if
的意图也比单标记c清楚得多ase声明,依我看。“!”…“/”
应该与您使用例如33..47所做的类似。我目前无法验证,因此这没有经过测试。我认为这不包括|和英镑符号。传说,这就像一个符咒,因为我已经将所有允许的符号添加到了该集合中。我最终单独添加了它们。这真的很有趣这是一个合适的解决方案。很高兴你选择它作为可接受的答案。<代码> ChanyStuts//Code >函数应该被用于Unicode Delphi来解决W1050编译器警告。但是我同意@杰瑞。我更喜欢这种方式的可读性。不客气!我错过了一件事。这里没有答案。u找到一个无效字符。asciicheck
变量,如果没有它,则获取字符串中最后一个字符的验证输出。asciicheck:=CharInSet(password[passwordlength],ValidChars)
本质上也会这样做,而不需要循环。
const
ValidChars = ['A'..'Z', 'a'..'z', '0'..'9'];
var
I: Integer;
begin
for I := 1 to passwordlength do
begin
if CharInSet(password[I], ValidChars) then
Writeln('valid') // more likely to do nothing and invert the if statement
else
begin
asciicheck := False;
Break; // No need to look further, the check failed
end;
end;
end;