效果图
输入框不可修改
设置edit控件的readonly属性为true。
生成随机数
Randomize 函数通过获取系统时钟的当前时间(或其他系统特定的随机源)来自动设置随机数生成器的种子。这样,每次程序运行时,由于系统时间的不同,都会得到不同的随机数序列。
Randomize;
edt1.Text:='';
for i:=1 to 10 do
begin
num[i] := 10 + Random(90);
edt1.Text:=edt1.Text+inttostr(num[i])+',';
end;
//去除末尾的逗号
edt1.Text:=copy(edt1.Text,1,length(edt1.Text)-1);
对随机数列排序
遍历数组来找到最大值。
max:=num[1];
for i:=2 to 10 do
begin
if (max<num[i]) then
begin
max:=num[i];
max_i:=i;
end;
end;
mp4转gif
网站在这,上一篇文章使用过。
完整代码
unit searchrandom;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Dialogs, Forms,Form, Formprpt, StdCtrls;
type
TForm1 = class(MForm)
edt1: TEdit;
btn1: TButton;
edt2: TEdit;
edt3: TEdit;
lbl1: TLabel;
lbl2: TLabel;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.btn1Click(Sender: TObject);
var
num:array[1..10] of Integer;
i:Integer; //for循环使用
max:Integer;//最大值
max_i:Integer;//最大值的下标
begin
Randomize;
edt1.Text:='';
for i:=1 to 10 do
begin
num[i] := 10 + Random(90);
edt1.Text:=edt1.Text+inttostr(num[i])+',';
end;
//去除末尾的逗号
edt1.Text:=copy(edt1.Text,1,length(edt1.Text)-1);
//找最大值及其下标
max:=num[1];
for i:=2 to 10 do
begin
if (max<num[i]) then
begin
max:=num[i];
max_i:=i;
end;
end;
edt2.Text:=inttostr(max);
edt3.Text:=inttostr(max_i);
end;
end.