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;
以下是一些需要注意的事项:

  • 不要使用相同的错误代码值来表示不同类型的错误。对不同的错误使用相同的值只会使您调试代码更加困难,因为您无法判断哪个测试给出了
    Error
    值1

  • 定义常量以表示不同类型的错误。这样,读者就不必想知道如果错误=3,在
    中“3是什么意思”…

  • 一旦您在密码中检测到一个数字字符,检查它后面的字符就没有意义了,因此我的
    for
    循环中的
    中断

  • 如果我是一个用户,在程序告诉我我做错了什么之前,如果没有人告诉我规则是什么,我会很恼火。事先告诉使用者规则是什么

  • 实际上,最好包括一个附加的常量
    Unclassified
    ,其值为-1,并通过将
    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;
以下是一些需要注意的事项:

  • 不要使用相同的错误代码值来表示不同类型的错误。对不同的错误使用相同的值只会使您调试代码更加困难,因为您无法判断哪个测试给出了
    Error
    值1

  • 定义常量以表示不同类型的错误。这样,读者就不必想知道如果错误=3,在
    中“3是什么意思”…

  • 一旦您在密码中检测到一个数字字符,检查它后面的字符就没有意义了,因此我的
    for
    循环中的
    中断

  • 如果我是一个用户,在程序告诉我我做错了什么之前,如果没有人告诉我规则是什么,我会很恼火。事先告诉使用者规则是什么

  • 实际上,最好包括一个附加的常量
    Unclassified
    ,其值为-1,并通过将
    Error
    分配给它来开始循环的每个迭代,在后续步骤中,测试
    Error=Unclassified
    而不是
    PWIsOk

  • case
    语句是一种整洁且易于维护的方法,可以根据序号值从多个互斥执行路径中选择一条


你有两张反对票和三张结束票。这可能是因为你的q没有一个关于密码中数字字符的明确说明,而且writeln文本不是英文的。如果你不更新你的q来解决这些问题,它很可能会获得两个以上的投票,然后没有人能够发布答案。正如你在简介中读到的,问答网站也是如此。在你的帖子里没有问题。你需要编辑你的帖子,清楚地说明你想问什么可能的问题,并将其表述为一个问题。正确的代码格式使其更易于阅读,如果代码中的文本对理解您的问题很重要,则必须使用英语。@Tom Brunberg根据您的建议编辑文章,谢谢您提供的信息。我很抱歉,如果一些帖子不符合这个网站的标准,因为我对它仍然很不熟悉。你有两张反对票和三张关闭票。这可能是因为你的q没有一个关于密码中数字字符的明确说明,而且writeln文本不是英文的。如果你不更新你的q来解决这些问题,它很可能会获得两个以上的投票,然后没有人能够发布答案。正如你在简介中读到的,问答网站也是如此。在你的帖子里没有问题。你需要编辑你的帖子,清楚地说明你想问什么可能的问题,并将其表述为一个问题。正确的代码格式使其更易于阅读,如果代码中的文本对理解您的问题很重要,则必须使用英语。@Tom Brunberg根据您的建议编辑文章,谢谢您提供的信息。我很抱歉,如果一些帖子不符合这个网站的标准,因为我仍然非常不熟悉它。非常感谢您的快速回复,我将在代码的其余部分尽量记住这些提示。让我们看看它是怎么回事:Dalso我想问为什么添加一个额外的常数比使用“PWIsOk”更好?@Nikolas:好吧,在这种情况下,没有太大的区别,但是根据经验,如果你有一系列更复杂的检查(比如,用不止一种方式得出PW是ok的结论),我想您会发现,从不同的值开始调试和检查错误会更容易。非常感谢您的快速响应,我将在代码的其余部分尽量记住这些提示。让我们看看它是怎么回事:Dalso我想问为什么添加一个额外的常数比使用“PWIsOk”更好?@Nikolas:好吧,在这种情况下,没有太大的区别,但是根据经验,如果你有一系列更复杂的检查(比如,用不止一种方式得出PW是ok的结论),我想您会发现,从不同的值开始调试和检查错误更容易。