全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:4201
推到 Plurk!
推到 Facebook!

if .... then 語法的問題

答題得分者是:christie
mnsf
初階會員


發表:104
回覆:90
積分:48
註冊:2003-11-25

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-12-25 10:14:36 IP:61.30.xxx.xxx 訂閱
var a:integer;
begin
if a in [1,2,3] then
showmessage('ok');
end;

假設以上 1,2,3 的個數是變動的,可能會是1,2,3或者1,2,3,4,
當a是integer時,可以用in的方式去寫,
那如果現在a是string的話,要用什麼方法才可以達到跟in一樣的效果呢
如果a是string,它的值可以是 'AA','BB','CC','DD'.....個數不固定,
那要怎麼寫呢?

var a:string
begin
if a ...... then
showmessage('ok');
end;
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-12-25 14:07:04 IP:203.73.xxx.xxx 未訂閱
//請用TStringList 的 IndexOf method
var
animals : TStringList; // Define our string list variable
i : Integer;
str1 : string;
begin
// Define a string list object, and point our variable at it
animals := TStringList.Create;
// Now add some names to our list
animals.Add('Cat');
animals.Add('Mouse');
animals.Add('Giraffe');
// Now display these animals
//for i := 0 to animals.Count-1 do
// ShowMessage(animals[i]);
ShowMessage(inttostr(animals.indexof('Cat')));
ShowMessage(inttostr(animals.indexof('Mouse')));
//indexof method
//Gets the index position of the first string matching the given string. Or -1 if not found
str1:='Giraffe';
if animals.IndexOf(str1) >=0 then
ShowMessage('Hello, str1 in the animals StringList.');
animals.Free
end;

Ref: http://www.delphibasics.co.uk/RTL.asp?Name=TStringList
------
What do we live for if not to make life less difficult for each other?
編輯記錄
christie 重新編輯於 2007-12-25 17:39:35, 註解 無‧
cobraliu
中階會員


發表:15
回覆:75
積分:83
註冊:2007-11-22

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-12-25 16:15:40 IP:59.127.xxx.xxx 訂閱
上面的大大以經有把方法做出來,自已是測試實做..

[code delphi]
procedure TForm1.Button1Click(Sender: TObject);
VAR
w_if :TStringList;
w_i :INteger;
begin
w_if := TStringList.Create;
w_if.Add('AA');
w_if.Add('BB');
FOR w_i:=0 TO w_if.Count-1 DO
IF edit1.Text = w_if.Strings[w_i] THEN
showmessage('OK');
w_if.Free;
end;
[/code]
------
初學、初學、學了很久...還是在初學階段..Orz
編輯記錄
cobraliu 重新編輯於 2007-12-25 16:57:43, 註解 無‧
mnsf
初階會員


發表:104
回覆:90
積分:48
註冊:2003-11-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-12-26 14:13:15 IP:61.30.xxx.xxx 訂閱
不好意思,我只是想問看看,有沒有一行就可以寫完的那種語法,
看來是沒有的樣子。
大大寫的也算是另一種方法啦,一樣謝謝嘍!!
系統時間:2024-03-29 13:05:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!