String Turbo Pascal:检查字符串是否包含数字
正如标题中所说,我很难找到如何检查字符串String Turbo Pascal:检查字符串是否包含数字,string,if-statement,pascal,turbo-pascal,String,If Statement,Pascal,Turbo Pascal,正如标题中所说,我很难找到如何检查字符串PW是否包含数字的解决方案。如果字符串PW包含数字,如何签入TP repeat writeln; writeln('Ok, please enter your future password.'); writeln('Attention: The Text can only be decoded with the same PW'); readln(PW); pwLength:
PW
是否包含数字的解决方案。如果字符串PW
包含数字,如何签入TP
repeat
writeln;
writeln('Ok, please enter your future password.');
writeln('Attention: The Text can only be decoded with the same PW');
readln(PW);
pwLength:= Length(PW);
error:=0;
for i:= 1 to Length(PW) do begin
if Input[i] in ['0'..'9'] then begin
error:=1;
end;
end;
if Length(PW)=0 then
begin
error:=1;
end;
if Length(PW)>25 then
begin
error:=1;
end;
if error=1 then
begin
writeln('ERROR: Your PW has to contain at least 1character, no numbers and has to be under 25characters long.');
readln;
clrscr;
end;
until error=0;
以下是我编写代码的方式:
var
PW : String;
Error : Integer;
const
PWIsOk = 0;
PWIsBlank = 1;
PWTooLong = 2;
PWContainsDigit = 3;
procedure CheckPassword;
var
i : Integer;
begin
writeln;
writeln('Ok, please enter your future password.');
writeln('Attention: The Text can only be decoded with the same PW');
writeln('Your password must be between 1 and 25 characters long and contain no digits.');
repeat
error := PWIsOk;
readln(PW);
if Length(PW) = 0 then
Error := PWIsBlank;
if Error = PWIsOk then begin
if Length(PW) > 25 then
Error := PWTooLong;
if Error = 0 then begin
for i := 1 to Length(PW) do begin
if (PW[i] in ['0'..'9']) then begin
Error := PWContainsDigit;
Break;
end;
end;
end;
end;
case Error of
PWIsOK : writeln('Password is ok.');
PWIsBlank : writeln('Password cannot be blank.');
PWTooLong : writeln('Password is too long.');
PWContainsDigit : writeln('Password should not contain a digit');
end; { case}
until Error = PWIsOk;
writeln('Done');
end;
以下是一些需要注意的事项:
- 不要使用相同的错误代码值来表示不同类型的错误。对不同的错误使用相同的值只会使您调试代码更加困难,因为您无法判断哪个测试给出了
值1Error
- 定义常量以表示不同类型的错误。这样,读者就不必想知道如果错误=3,在
中“3是什么意思”…
- 一旦您在密码中检测到一个数字字符,检查它后面的字符就没有意义了,因此我的
循环中的for
中断
- 如果我是一个用户,在程序告诉我我做错了什么之前,如果没有人告诉我规则是什么,我会很恼火。事先告诉使用者规则是什么
- 实际上,最好包括一个附加的常量
,其值为-1,并通过将Unclassified
分配给它来开始循环的每个迭代,在后续步骤中,测试Error
而不是Error=Unclassified
PWIsOk
语句是一种整洁且易于维护的方法,可以根据序号值从多个互斥执行路径中选择一条case
var
PW : String;
Error : Integer;
const
PWIsOk = 0;
PWIsBlank = 1;
PWTooLong = 2;
PWContainsDigit = 3;
procedure CheckPassword;
var
i : Integer;
begin
writeln;
writeln('Ok, please enter your future password.');
writeln('Attention: The Text can only be decoded with the same PW');
writeln('Your password must be between 1 and 25 characters long and contain no digits.');
repeat
error := PWIsOk;
readln(PW);
if Length(PW) = 0 then
Error := PWIsBlank;
if Error = PWIsOk then begin
if Length(PW) > 25 then
Error := PWTooLong;
if Error = 0 then begin
for i := 1 to Length(PW) do begin
if (PW[i] in ['0'..'9']) then begin
Error := PWContainsDigit;
Break;
end;
end;
end;
end;
case Error of
PWIsOK : writeln('Password is ok.');
PWIsBlank : writeln('Password cannot be blank.');
PWTooLong : writeln('Password is too long.');
PWContainsDigit : writeln('Password should not contain a digit');
end; { case}
until Error = PWIsOk;
writeln('Done');
end;
以下是一些需要注意的事项:
- 不要使用相同的错误代码值来表示不同类型的错误。对不同的错误使用相同的值只会使您调试代码更加困难,因为您无法判断哪个测试给出了
值1Error
- 定义常量以表示不同类型的错误。这样,读者就不必想知道如果错误=3,在
中“3是什么意思”…
- 一旦您在密码中检测到一个数字字符,检查它后面的字符就没有意义了,因此我的
循环中的for
中断
- 如果我是一个用户,在程序告诉我我做错了什么之前,如果没有人告诉我规则是什么,我会很恼火。事先告诉使用者规则是什么
- 实际上,最好包括一个附加的常量
,其值为-1,并通过将Unclassified
分配给它来开始循环的每个迭代,在后续步骤中,测试Error
而不是Error=Unclassified
PWIsOk
语句是一种整洁且易于维护的方法,可以根据序号值从多个互斥执行路径中选择一条case