如何在 Delphi 10.3 上不接受字符串中的特殊字符

How to not accept special characters in a string on Delphi 10.3

我正在尝试为我的学校作业编写一个程序。它应该接受名字为 strings,薪水输入为 integer,并告诉你是否难以辨认或是否能够参加派对部分,并重复直到 stop=9

一切顺利,但问题是 string 变量接受错误的名称作为输入并继续。例如 'John32@#$^^'.

我希望程序不接受这些输入,并写一行声明它不会继续使用除字符以外的任何名称。这应该还没有加号就停止。源代码如下。

program Sba;

Uses
  Windows;

var
  FName,LName:String;
  Stop,Salary:Integer;

//procedure Sleep(milliseconds: Cardinal); stdcall; external 'kernel32.dll'; //Added sleep as a procedure for delay

Begin
  Repeat //Repeats until stop = 9
    Writeln('Hi User,please Enter your First Name'); //Prompting user to enter their First initials
    Readln(FName); //Attaches the string entered to the variable FName
    Sleep(1000);//Delay 1second
    Writeln('First Name entered');  //Lets the user know they have entered their first initials
    Sleep(1000);
    Writeln('Please Enter your Last name'); //Prompting user to enter their last initial
    Readln(LName);  //Attaches the string entered to the variable LName
    Sleep(1000);
    Writeln('Last Name entered'); //Lets the user know they have entered their last initials
    Sleep(800);
    Writeln('Hi ',' ',FName,' ',LName); //Writes Line Hi (First Name) (Last Name)
    Sleep(1000);
    Writeln('Please enter The amount you have paid');//Prompts user for their payment
    Readln(Salary); //Atttached value entered to Salary
    Writeln('Reading your payment will be done in four seconds');
    Sleep(4000);//delay 4 seconds
    Writeln('Done!');
    If (Salary<1000) Then
    Begin
      Writeln('You do not have sufficient funds to play in a section');
    Sleep(1000);
    End;
    Sleep(1000);
    If (Salary>=1000) AND (Salary<=2000) then //Testing conditions
    Begin
      Sleep(1000);
      Writeln('Congrats, You are eligible to play in the Tribe Section'); //Tells use what section they are available to play in
    End;
    If (Salary>=2100) AND (Salary<=3000) then //Testing conditions
    Begin
      Sleep(1000);
      Writeln('Congrats, You are eligible to play in the Poison Section');
    End;
    If (Salary>=3100) AND (Salary<=5000) then //Testing conditions
    Begin
      Sleep(1000);
      Writeln('Congrats, You are eligible to play in the Harts Section');
    End;
    Writeln('Press enter to continue');
    Readln;
    Writeln('Enjoy your day ' , FName,'!');
    Stop:=Stop+1; //Repeats until stop= 9
    Sleep(1000);
  Until Stop=9;//Repeats this 10 times
  Writeln('Written By Timothy Adams 4-1');
  Readln;
end.

因为这是作业,所以我不会完全按照你的要求给你。相反,我会给你一个更复杂的例子来研究。

首先,我们必须决定一个有效的名字是什么样的。因为如果我们不确切地知道哪些名称被认为是有效的,我们肯定无法通过编程来区分计算机!

举个例子,假设一个名字是有效的 iff:

  1. 它只包含字母、空格和 HYPHEN-MINUS (-) 字符。 [注意字符串中的所有内容都是一个字符。字符 类 的示例包括字母、数字、空格和标点符号。所以当你说字符串必须只包含 "characters" 时,你的意思是别的东西,比如 "only letters".]

  2. 至少包含两个字母

这对于实际应用来说还不够好。它会禁止许多有效名称。但对于这个玩具示例来说已经足够了。

让我们来实施吧!

function IsValidName(const S: string): Boolean;
var
  i, c: Integer;
begin

  // The string must only contain letters, whitespace, and HYPHEN-MINUS

  for i := 1 to S.Length do
    if not (S[i].IsLetter or S[i].IsWhiteSpace or (S[i] = '-')) then
      Exit(False);

  // The string must contain at least two letters

  c := 0;
  for i := 1 to S.Length do
    if S[i].IsLetter then
      Inc(c);

  if c < 2 then
    Exit(False);

  Result := True;

end;

研究这个函数,直到你完全理解它是如何工作的!

完成了吗?伟大的!现在让我们使用它:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Character;

var
  Name: string;

function IsValidName(const S: string): Boolean;
begin
  // same as above, won't repeat it here
end;

begin
  try
    try

      Writeln('Hello! What is your name?');

      while True do
      begin
        Readln(Name);
        Name := Name.Trim;
        if IsValidName(Name) then
        begin
          Writeln('Welcome, ', Name, '!');
          Break;
        end
        else
          Writeln('Surely that isn''t your real name? What is your actual name?');
      end;

    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    Writeln('Program about to end. Press Return to exit.');
    Readln;
  end;
end.

再说一次,因为我给你做作业差点违反规则,我就不详细解释逻辑了。相反,我会让你自己弄清楚它是如何工作的。这样你会学到更多。

更新:完整的程序应该是这样的:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Character;

var
  Name: string;

function IsValidName(const S: string): Boolean;
var
  i, c: Integer;
begin

  // The string must only contain letters, whitespace, and HYPHEN-MINUS

  for i := 1 to S.Length do
    if not (S[i].IsLetter or S[i].IsWhiteSpace or (S[i] = '-')) then
      Exit(False);

  // The string must contain at least two letters

  c := 0;
  for i := 1 to S.Length do
    if S[i].IsLetter then
      Inc(c);

  if c < 2 then
    Exit(False);

  Result := True;

end;

begin
  try
    try

      Writeln('Hello! What is your name?');

      while True do
      begin
        Readln(Name);
        Name := Name.Trim;
        if IsValidName(Name) then
        begin
          Writeln('Welcome, ', Name, '!');
          Break;
        end
        else
          Writeln('Surely that isn''t your real name? What is your actual name?');
      end;

    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    Writeln('Program about to end. Press Return to exit.');
    Readln;
  end;
end.