unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
var
  arr:array[0..9] of Integer;
procedure RandomNum;
var
  i,k,n:Integer;
  flag:Boolean;
begin
  arr[0]:=Random(10);
  for I := 1 to 9 do {for语句:保存互不相同的随机数到数组}
    begin
      flag:=True;
      while flag do  {while语句:循环算出不同于任一已存在数组元素的数值}
      begin
        n:=Random(10);
        flag:=False;
        for k := 0 to i - 1 do  {此for语句:将随机数n与已存在的数组元素作比较}
        begin
          if arr[k] = n then
          begin
            flag:=True;
            Continue;
          end;
        end;
      end;
      arr[i]:=n;
    end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  RandomNum;
  Edit1.Text:=IntToStr(arr[0]);
  Edit2.Text:=IntToStr(arr[1]);
  Edit3.Text:=IntToStr(arr[2]);
  Edit4.Text:=IntToStr(arr[3]);
  Edit5.Text:=IntToStr(arr[4]);
  Edit6.Text:=IntToStr(arr[5]);
  Edit7.Text:=IntToStr(arr[6]);
  Edit8.Text:=IntToStr(arr[7]);
  Edit9.Text:=IntToStr(arr[8]);
  Edit10.Text:=IntToStr(arr[9]);
end;

end.
posted on 2011-01-11 12:20  巅枫  阅读(470)  评论(0编辑  收藏  举报