Delphi入门,if..else随机加减法小程序

打包下载:https://files.cnblogs.com/qqook/p1.rar

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    edt1: TEdit;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure edt1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
  var
    x,y,o:Integer;
begin
     Randomize;
     x:= Random(50);
     y:= Random(50);
     o:= Random(2);
     lbl1.Caption := IntToStr(x);
     lbl3.Caption := IntToStr(y);
     if o > 0 then
      lbl2.Caption := '+'
     else
      lbl2.Caption := '-';
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  close;
end;

procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
  var
    answer:Integer;
begin
    if Key = Chr(VK_RETURN)  then
      begin
        if lbl2.Caption = '+' then
        begin
           answer := StrToInt(lbl1.Caption) + StrToInt(lbl3.Caption) ;
        end
        else
        begin
          answer := StrToInt(lbl1.Caption) - StrToInt(lbl3.Caption)
        end;
        if answer = StrToInt(edt1.Text)  then
        begin
          ShowMessage('回答正确');
          btn1.SetFocus;
        end
        else
        begin
           ShowMessage('回答错误');
           edt1.SetFocus;
           edt1.SelectAll;
        end;
      end;
end;

end.
posted @ 2013-12-09 22:28  內個誰²º¹³  阅读(487)  评论(0)    收藏  举报