[英]How to not accept special characters in a string on Delphi 10.3
I am trying to write a program for my school-based assignment.我正在尝试为我的学校作业编写一个程序。 It is supposed to accept names as string
s, and a salary input as an integer
, and tell you whether you are illegible or able to participate in party sections, and repeats until stop=9
.它应该接受名称为string
s,薪水输入为integer
,并告诉您您是否难以辨认或能够参加聚会部分,并重复直到stop=9
。
Everything went well, but the issue is the string
variables accept erroneous names as inputs and continue.一切顺利,但问题是string
变量接受错误的名称作为输入并继续。 For example 'John32@#$^^'
.例如'John32@#$^^'
。
I want the program to not accept these inputs, and write a line stating that it will not continue with any name with anything other than characters.我希望程序不接受这些输入,并写一行说明它不会继续使用除字符以外的任何名称。 This should also not add a number to stop.这也不应该添加一个数字来停止。 The source code is below.源代码如下。
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.
Since this is homework, I will not give you exactly what you ask for.由于这是家庭作业,我不会给你确切的要求。 Instead, I'll give you a somewhat more involved example to study.相反,我会给你一个更复杂的例子来研究。
First, we must decide what a valid name looks like.首先,我们必须确定一个有效的名称是什么样的。 Because if we don't know exactly what names are to be considered valid, we surely cannot program a computer to tell the difference!因为如果我们不确切知道哪些名称被认为是有效的,我们肯定无法对计算机进行编程来区分它们!
Just as a toy example, let's say that a name is valid iff :就像一个玩具例子,假设一个名字是有效的iff :
It contains only letters, whitespace, and the HYPHEN-MINUS (-) character.它仅包含字母、空格和连字符减号 (-) 字符。 [Notice that everything in a string is a character. [请注意,字符串中的所有内容都是一个字符。 Examples of character classes are letters, digits, whitespace, and punctuation.字符类的示例是字母、数字、空格和标点符号。 So when you say that the string must only contain "characters", you mean something else, like "only letters".]所以当你说字符串必须只包含“字符”时,你的意思是别的东西,比如“只有字母”。]
It contains at least two letters.它至少包含两个字母。
This is by far not good enough for a real-world application.到目前为止,这对于现实世界的应用程序来说还不够好。 It would disallow many valid names.它将不允许许多有效名称。 But it is good enough for this toy example.但这对于这个玩具示例来说已经足够了。
Let's implement this!让我们实现它!
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;
Study this function until you fully understand how it works!研究这个 function 直到你完全理解它是如何工作的!
Done?完毕? Great: Now let's make use of it:太好了:现在让我们使用它:
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.
Again, since I am almost breaking the rules by doing your homework for you, I won't explain the logic in detail.再说一次,因为我为你做作业几乎是在打破规则,所以我不会详细解释其中的逻辑。 Instead, I'll let you figure out how it works for yourself.相反,我会让你弄清楚它是如何为自己工作的。 That way you will learn a lot more.这样你会学到更多。
Update: The complete program should look like this:更新:完整的程序应该是这样的:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.