小
小唐
Unregistered / Unconfirmed
GUEST, unregistred user!
不会吧,可能是你粘贴时没有弄好,下面是完整的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function RandomSelectString(iCount: integer;
SomeString: string): string;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.RandomSelectString(iCount: integer
SomeString : string): string;
var
sl : TStringList;
str : string;
iRand,i : integer;
begin
sl :=TStringList.create;
//根据iCount的值取出SomeString中的字符串
for i :=0 to iCount-1 do
begin
if i<iCount-1 then
str :=copy(someString,1,pos(',',SomeString)-1)
else
str :=SomeString;
sl.add(str);
if i<iCount-1 then
begin
system.delete(someString,1,pos(',',SomeString));
end;
end;
//利用随机数来求出随机字符串
iCount :=sl.count;
Randomize;
iRand :=Random(iCount);
Result :=sl.Strings[iRand];
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showMessage(RandomSelectString(6,'1白猫,2黑猫,3野猫,4波斯猫,5黄鼠锒,6老虎'));
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function RandomSelectString(iCount: integer;
SomeString: string): string;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.RandomSelectString(iCount: integer
SomeString : string): string;
var
sl : TStringList;
str : string;
iRand,i : integer;
begin
sl :=TStringList.create;
//根据iCount的值取出SomeString中的字符串
for i :=0 to iCount-1 do
begin
if i<iCount-1 then
str :=copy(someString,1,pos(',',SomeString)-1)
else
str :=SomeString;
sl.add(str);
if i<iCount-1 then
begin
system.delete(someString,1,pos(',',SomeString));
end;
end;
//利用随机数来求出随机字符串
iCount :=sl.count;
Randomize;
iRand :=Random(iCount);
Result :=sl.Strings[iRand];
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showMessage(RandomSelectString(6,'1白猫,2黑猫,3野猫,4波斯猫,5黄鼠锒,6老虎'));
end;
end.