请教一下randomfrom这个函数怎样使用!(50分)

  • 主题发起人 主题发起人 claire_cn
  • 开始时间 开始时间
C

claire_cn

Unregistered / Unconfirmed
GUEST, unregistred user!
多谢了!
 
randomize;
randomfrom(传一个数组)
看了帮助应该时这个意思
 
能不能写一个简单的例子?
 
首部 function RandomFrom(const AValues: array of string): string; overload; $[StrUtils.pas
功能 随机返回字符串数组AValues中的一个元素
说明 之前建议执行Randomize
参考 function System.Random
例子 Randomize; Edit3.Text := RandomFrom(['a1', 'a2', 'a3', 'a4']);
 
运行时出错,提示出错:
[Error] Unit1.pas(32): Ordinal type required
[Error] Unit1.pas(32): Incompatible types: 'Integer' and 'String'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

原码如下:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Randomize;
Edit1.Text := RandomRange(['a1', 'a2', 'a3', 'a4']);

end;

end.


不知是什么原因,请高手们指点!
 

/////注意添加StrUtils单元。下面的源码通过。
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Randomize;
label1.Caption:=RandomFrom(['a1', 'a2', 'a3', 'a4']);
end;

end.
 
十分感谢!
 
后退
顶部