2016-03-31 99 views
1

因此,我正在研究一個程序,我想搜索某個單詞,但我並不十分確定如何設置搜索功能。這正是我試圖使用的,但它所做的只是輸出我輸入的任何內容。Arraylist搜索

static void searchWords(ArrayList arlWords) 
    { 
     string strInput; 

     Console.Write("Search a Word: "); 
     strInput = Console.ReadLine(); 
     arlWords.IndexOf(strInput, 0); 
     Console.WriteLine("{0}", strInput); 
    } 
+0

對於某個單詞,「*」的含義是什麼意思:您計劃在之後計劃如何處理它? –

+0

它只是搜索我已經放入的單詞,並告訴我,如果我有它或不是 –

+0

因爲您的方法需要在列表中,您可以開始編寫代碼以找到該單詞在該「列表」 – starcorn

回答

1

請試試這個:

static void searchWords(ArrayList arlWords) 
{ 
    string strInput; 
    bool check= false; 
    Console.Write("Search a Word: "); 
    strInput = Console.ReadLine(); 
    for (int i = 0; i < arlWords.Items.Count; i++) 
    { 
    if (arlWords.Items[i] == strInput) 
     check = true; 
    } 
    if(check) Console.WriteLine("found"); 
    else Console.WriteLine("not found"); 
} 
+0

arrSample從哪裏來? –

+0

感謝您的警告,修復 –

1

您已經定義它來寫 'strInput',這樣它會寫。相反,您應該捕獲'IndexOf'的結果並打印 - 或者它是否大於0。