For语句应用判断是否为素数
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Edit1: TEdit; Edit2: TEdit; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); //“判断”按钮 var s,n,i,j: integer; m,q: real; a: string; begin if edit1.Text = '' then showmessage('不能为空') //错误判断 else begin m := strtofloat(edit1.Text); n := round(m); if n < 3 then showmessage('请输入一个大于2的整数!'); s := 0; q := sqrt(m); //开方 j := trunc(q); //取整 for i := 2 to j do begin if n mod i = 0 then //取余判断 s:= 1; if s = 0 then //判断是否为素数 a := '是一个素数' else a := '不是素数'; edit2.Text := edit1.Text + a; end; end; edit1.SelectAll; //编辑框1全选 edit1.SetFocus; //光标设定在编辑框1 end; procedure TForm1.Button2Click(Sender: TObject); //“清除”按钮 begin edit1.Text := ''; edit2.Text := ''; end; end.