点击一个button1按扭把edit1-edit2范围内的所有质数添加到一个Listbox1中去
我刚开始学 不太会 有会的把代码都写上 谢谢
參考答案:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function IsPrime (N: LongInt): Boolean;
var
Test: LongInt;
begin
IsPrime := True;
for Test := 2 to N - 1 do
begin
if (N mod Test) = 0 then
begin
IsPrime := False;
break; {jump out of the for loop}
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i,iBegin,iEnd:Integer;
begin
try
iBegin := strtoint(edit1.text);
except
showmessage('请在Edit1内填入数字!');
Exit;
end;
try
iEnd := strtoint(edit2.text);
except
showmessage('请在Edit2内填入数字!');
Exit;
end;
if iBegin>iEnd then
begin
ShowMessage('edit2中的数字比edit1中的小!,请重新输入!');
exit;
end;
ListBox1.Items.Clear;
for i := iBegin to iEnd do
begin
if IsPrime(i) then
ListBox1.Items.Add(IntToStr(i));
end;
end;
end.