|
发表于 2014-5-5 15:34:11
|
显示全部楼层
- unit Unit1;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
- type
- { TForm1 }
- TForm1 = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- tishi: TLabel;
- cishu: TLabel;
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: char);
- procedure FormCreate(Sender: TObject);
- private
- { private declarations }
- public
- { public declarations }
- end;
- var
- Form1: TForm1;
- number,i,j,guess,ga,gb,times:longint;
- a,g:array[1..4] of byte;
- flag:boolean;
- ss,sok:string;
- implementation
- {$R *.lfm}
- { TForm1 }
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- //初始化
- edit1.caption:='';
- tishi.caption:='提示:';
- cishu.Caption:='次数:';
- //给出四位不重复随机数
- randomize;
- flag:=false;
- repeat
- for i:=1 to 4 do a[i]:=random(9)+1;
- if (a[1]<>a[2])and(a[1]<>a[3])and(a[1]<>a[4])and
- (a[2]<>a[3])and(a[2]<>a[4])and(a[3]<>a[4]) then flag:=true;
- until flag;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- ga:=0;gb:=0;
- ss:=edit1.caption;
- //出错处理
- for i:=1 to length(ss) do
- if not(ss[i] in ['1'..'9']) then
- begin
- Showmessage('输入有误,请输入一个四位数。');
- exit;
- end;
- guess:=strToint(ss);
- if (guess<1000) or (guess>9999) then Showmessage('输入有误,请输入一个四位数。');
- //取数字
- g[1]:=guess div 1000;g[2]:=guess div 100 mod 10;
- g[3]:=guess div 10 mod 10;g[4]:=guess mod 10;
- //数字比较
- for i:=1 to 4 do
- for j:=1 to 4 do
- if (g[i]=a[j]) then
- begin
- if i=j then inc(ga) else inc(gb);
- end;
- inc(times); //增加猜测次数
- sok:='恭喜您,猜对了!用了'+intTostr(times)+'次';
- if ga=4 then begin Showmessage(sok);exit;end; //猜对退出
- //猜错给出提示及猜测次数
- Form1.tishi.Caption:='提示:'+intTostr(ga)+'A'+intTostr(gb)+'B';
- form1.cishu.Caption:='次数:'+intTostr(times);
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
- begin
- if key=chr(13) then Button1.Click;
- end;
- end.
复制代码 lazarus实现
|
|