unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function countchip(str:string):integer;
end;var
Form1: TForm1;implementation{$R *.dfm}
function Tform1.CountChip(str:string): integer;
var
i, j, timesCount: integer;
s, temp:string;
begin
timesCount := 1;
while length(str)>0 do
begin
i := pos('=',str);
j := pos(',',str);
s := copy(str,i+1,j-1);
if length(s)>1 then
timesCount := timesCount * 2;
str := stuffstring(str,1,j,'');
end;
result := timesCount;
showmessage(inttostr(timescount));
end;procedure TForm1.Button1Click(Sender: TObject);
var
i, j: integer;
s: string;
begin
s := '100608001=3';
form1.countchip(s);
end;end.
运行后程序不动了,跪求指点1!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function countchip(str:string):integer;
end;var
Form1: TForm1;implementation{$R *.dfm}
function Tform1.CountChip(str:string): integer;
var
i, j, timesCount: integer;
s, temp:string;
begin
timesCount := 1;
while length(str)>0 do
begin
i := pos('=',str);
j := pos(',',str);
s := copy(str,i+1,j-1);
if length(s)>1 then
timesCount := timesCount * 2;
str := stuffstring(str,1,j,'');
end;
result := timesCount;
showmessage(inttostr(timescount));
end;procedure TForm1.Button1Click(Sender: TObject);
var
i, j: integer;
s: string;
begin
s := '100608001=3';
form1.countchip(s);
end;end.
运行后程序不动了,跪求指点1!
while 语句进入死循环了
好好检查下你的While中断的条件
function Tform1.CountChip(str:string): integer;
var
i, j, timesCount: integer;
s, temp:string;
begin
timesCount := 1;
while length(str)>0 do
begin
i := pos('=',str);
j := pos(',',str);
//加上这段,避免找不到的时候不停的循环
if (i=0) or (j=0) then
begin
str:='';
Continue;
end;
//***********************
s := copy(str,i+1,j-1);
if length(s)>1 then
timesCount := timesCount * 2;
str := stuffstring(str,1,j,'');
end;
result := timesCount;
showmessage(inttostr(timescount));
end;