文章

Delphi7 获取10个随机数并显示最大值和其下标

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetWindowLong(Edit1.handle, GWL_STYLE, GetWindowLong(Edit1.Handle,GWL_STYLE)+ES_CENTER);
  Edit1.Refresh;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  RunNum:Array[1..10] of Integer;
  i:Integer;
  Max,Maxi:Integer; //Max为最大数字,Maxi为最大数字下标
begin
  Randomize;
  Edit1.text:= '';
  Edit2.Text:= '';
  For i:=1 to 10 do
  begin
    RunNum[i]:=10 + Random(90);
    Edit1.Text:=Edit1.Text + IntToStr(RunNum[i]) + ',' ;
  end;
  Edit1.Text:=Copy(Edit1.Text,1,Length(Edit1.Text)-1);
  Max:=RunNum[1];  //假设第一个下标的值为最大值
  Maxi:=1;
  For i:=2 to 10 do
  begin
      If Max < RunNum[i] then
      begin
        Max:=RunNum[i];
        Maxi:=i;
      end
  end;
  Edit2.Text:=IntToStr(Max);
  Edit3.text:=IntToStr(Maxi);
end;

end.

原文来自:Delphi7 获取10个随机数并显示最大值和其下标,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论