你相信吗?多一个ShowMessage结果就截然不同了!!!欢迎讨论!!!
qiume 2004-05-12 02:52:22 // 大家仔细看下面的代码,最好运行一遍,看看结果,是不是比较晕?
//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GenerateRandomNum(const Count: Integer): string;
function GenerateRandomNum2(const Count: Integer): string;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.GenerateRandomNum(const Count: Integer): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Count do
begin
Randomize;
Result := Result + IntToStr(Random(2));
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GenerateRandomNum(10)); // 只有 0000000000 和 1111111111 两种结果;
ShowMessage(GenerateRandomNum2(10)); // 我想要得结果;
end;
function TForm1.GenerateRandomNum2(const Count: Integer): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Count do
begin
Randomize;
Result := Result + IntToStr(Random(2));
ShowMessage(Result); // 只多了这么一行,结果......!!!
end;
end;
end.