不论我添的是什么都提示我是对的,为什么啊??????function HasNum(s:string):bool;
var i:integer;
begin
for i := 1 To Length(s) do
if (s[i] >= '0') or (s[i] <= '9') or (s[i] = '+') then
begin
HasNum := True;
Exit;
end
else
begin
HasNum := FALSE;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ss:string;
begin
if ed_ydz.Text<>'' then //Ô´µØÖ·*******
ss:=ed_ydz.Text;
if HasNum(ss) then
begin
MessageBox(0,正确·',MB_ICONERROR);
end
else
begin
MessageBox(0,错误,'´íÎó',MB_ICONERROR);
end;
end;
var i:integer;
begin
for i := 1 To Length(s) do
if (s[i] >= '0') or (s[i] <= '9') or (s[i] = '+') then
begin
HasNum := True;
Exit;
end
else
begin
HasNum := FALSE;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ss:string;
begin
if ed_ydz.Text<>'' then //Ô´µØÖ·*******
ss:=ed_ydz.Text;
if HasNum(ss) then
begin
MessageBox(0,正确·',MB_ICONERROR);
end
else
begin
MessageBox(0,错误,'´íÎó',MB_ICONERROR);
end;
end;
var i:integer;
begin
for i := 1 To Length(s) do
if (s[i] >= '0') or (s[i] <= '9') or (s[i] = '+') then
begin
HasNum := True;
Exit;
end
else
begin
HasNum := FALSE;
end;
end;你这么循环,除非你传如的字符串中没有一个数字和+号才会返回false的。呵呵
if (s[i] >= '0') and (s[i] <= '9') or (s[i] = '+') then
var i:integer;
begin
for i := 1 To Length(s) do
if (s[i] >= '0') or (s[i] <= '9') or (s[i] = '+') then
begin
HasNum := True;
Exit;
end
else
begin
HasNum := FALSE;
Break; //加这句
end;
end;
第一个or不对了,要改成and
if (s[i] >= '0') and(s[i] <= '9') or (s[i] = '+') then function HasNum(s:string):bool;
var i:integer;
begin
Result:=false;
for i := 1 To Length(s) do
if s[i] in ['+','0'..'9'] then
begin
HasNum := True;
Brake;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
begin
s:= '0099++a';
if ss(s) then showmessage('true') else showmessage('false');
end;function TForm1.ss(s: string): boolean;
var
i: integer;
a: string;
begin
result:= true;
for i:= 1 to length(s) do
begin
if not (s[i] in ['0'..'9','+']) then
begin
result:= false;
break;
end;
end;
end;