線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1594
推到 Plurk!
推到 Facebook!

抽獎不重覆

答題得分者是:pceyes
lisahsu7488
一般會員


發表:9
回覆:3
積分:2
註冊:2007-10-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-01-30 19:54:54 IP:210.243.xxx.xxx 訂閱
各位大大,真的很不好意思…小妹已經盡力了…
很想寫出一支每抽出一個數字就會記錄到listbox中
但抽出的數字不可以和存在listbox中的數字一樣
目前已經可以儲存到listbox了
但判斷的地方一直寫不出來
可以麻煩各位大大提示一下如何判斷嗎??
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
total: TEdit;
Label4: TLabel;
over: TEdit;
Button1: TButton;
Label5: TLabel;
ListBox1: TListBox;
procedure totalKeyPress(Sender: TObject; var Key: Char);
procedure Button1Click(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
a:array[1..100] of integer;
implementation
{$R *.dfm}
procedure TForm1.totalKeyPress(Sender: TObject; var Key: Char);
begin
if (key=#13)then begin
over.Text:=total.text;
button1.SetFocus;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i,temp,r:integer;
total1:integer; //總球數
over1:integer; //剩下的球數
begin
total1:=strtoint(total.text);
over1:= strtoint(over.text);
randomize;
for i:=1 to total1 do
begin
a[i]:=i;
end;
r:=(random(total1-1)) 1 ;
temp:=a[i];
a[i]:=a[r];
a[r]:=temp;
label5.caption:= inttostr(a[i]);
listbox1.Items.add(inttostr(a[i]));
over1:=over1-1;
over.text:=inttostr(over1);
end;
end.
編輯記錄
taishyang 重新編輯於 2008-01-31 15:09:01, 註解 將文章分類成[問題]‧
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-01-30 21:02:38 IP:122.127.xxx.xxx 訂閱

[code delphi]
if listbox1.Items.IndexOf(inttostr(a[i]))=-1 then
listbox1.Items.add(inttostr(a[i]));
[/code]

查 help indexof 用法
Description
Call IndexOf to obtain the position of the first occurrence of the string S, or of a string that differs from S only by case. IndexOf returns the 0-based index of the string. Thus, if S matches the first string in the list, IndexOf returns 0, if S is the second string, IndexOf returns 1, and so on. If the string is not in the string list, IndexOf returns -1.
Note: If the string appears in the list more than once, IndexOf returns the position of the first occurrence.

------
努力會更接近成功
lisahsu7488
一般會員


發表:9
回覆:3
積分:2
註冊:2007-10-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-02-04 11:09:04 IP:210.243.xxx.xxx 訂閱
謝謝大大的幫忙,最後小妹寫出來囉!大致是這個樣子…
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, jpeg;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
total: TEdit;
Label4: TLabel;
over: TEdit;
Button1: TButton;
Label5: TLabel;
listbox1: TListBox;
Button2: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
list:array[1..100] of integer;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
r,a,i:integer;
over1:integer;
label aa;
begin
for i := 1 to 100 do begin
sleep(10);
application.ProcessMessages;
label5.Caption := format('%.3d',[random(999)]);
end;
a:=0;
while true do
begin
r:=random(strtoint(total.text));
if list[r]=0 then
begin
list[r]:=1;
listbox1.items.add(inttostr(r 1));
label5.Caption:=inttostr(r 1); //r 1是中獎號數,r是它在array的位置
over1:=strtoint(over.text)-1;
over.text:=inttostr(over1);
goto aa;
end;
a:=a 1;
if a>100 then
begin
label5.caption:='!!沒獎可抽了!!';
goto aa;
end;
end;
aa:
end;
procedure TForm1.FormCreate(Sender: TObject);
var
a:integer;
filehandle:integer;
begin

for a:=0 to 100-1 do
begin
list[a]:=0;
randomize;
end;
//取檔放資料入array
if fileexists('filename') then
begin
ListBox1.Items.LoadFromFile('filename');
for a:=0 to ListBox1.Items.Count-1 do
Begin
list[StrToInt(ListBox1.Items.Strings[a])-1]:=1;
end;
total.text:=inttostr(100);
over.text:=inttostr(100-listbox1.Items.Count);
end else
begin
filehandle:=filecreate('filename');
fileclose(filehandle);
over.text:=inttostr(100);
total.text:=inttostr(100);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//從這邊開始存檔
ListBox1.Items.SaveToFile('filename');
close();
end;
end.
系統時間:2024-04-24 4:37:10
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!