ObjectPascal:随机数加减法口算测试;
procedure TForm1.FormCreate(Sender: TObject);
var m,n,k:integer;
begin
randomize;
m:=random(50);
n:=random(50);
k:=random(2);
label1.Caption:=inttostr(m);
label2.Caption:=inttostr(n);
label3.Caption:=inttostr(k);
if k=1 then
label2.Caption:='+'
else
label2.Caption:='-';
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var answer:integer;
begin
if key=chr(VK_RETURN) then
begin
if label2.Caption='+' then
answer:=strtoint(label1.Caption)+strtoint(label3.Caption)
else
answer:=strtoint(label1.Caption)-strtoint(label3.Caption);
if answer=strtoint(edit1.Text) then
begin
showmessage('回答正确');
button1.SetFocus;
end
else
begin
showmessage('回答错误');
edit1.SetFocus;
end;
edit1.SelectAll;
end;
end;