计算阶乘,可按回车键计算
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function jiecheng(n:integer):integer;
begin
if (n=0) or (n=1) then
result:=1
else
result:=jiecheng(n-1)*n;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
if (strtoint(edit1.Text)>=0) and (strtoint(edit1.Text)<10) then
edit2.Text:=inttostr(jiecheng(strtoint(edit1.text)))
else
begin
edit2.Clear;
showmessage('输入数不在0-9之间');
end;
except
showmessage('输入数错误');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=vk_return then
try
if (strtoint(edit1.Text)>=0) and (strtoint(edit1.Text)<10) then
edit2.Text:=inttostr(jiecheng(strtoint(edit1.text)))
else
begin
edit2.Clear;
showmessage('输入数不在0-9之间');
end;
except
showmessage('输入数错误');
end;
end;
end.