當前位置:成語大全網 - 新華字典 - C#字典程序的查詢問題

C#字典程序的查詢問題

樓上回答的不太對,題目只說查找帶“色”字的,沒說以“色”結尾的

我的代碼是這樣

?var?openwith=?new?Dictionary<string,string>();

openwith.Add("red","紅色");

openwith.Add("blue","藍色");

openwith.Add("apple","蘋果");

//查找所有目標項

var?resultItem?=?openwith.Where(c?=>?c.Value.Contains("色"));

//循環輸出結果

foreach?(var?keyValuePair?in?resultItem)

{

Console.WriteLine(keyValuePair.Key);

}

Console.ReadLine();

附上截圖:

以及額外的版本和截圖:

var?openwith=?new?Dictionary<string,string>();

openwith.Add("red","紅色");

openwith.Add("blue","藍色");

openwith.Add("apple","蘋果");

Console.WriteLine("請輸入要查詢的單詞:");

var?words?=?Console.ReadLine();

while?(string.IsNullOrEmpty(words))

{

Console.WriteLine("輸入的字符為空,請重新輸入:");

words?=?Console.ReadLine();

}

//去除空格

words?=?words.Trim();

if?(openwith.Values.Any(c=>c.Contains(words)))

{

var?resultItem?=?openwith.Where(c?=>?c.Value.Contains(words));

Console.WriteLine("查找完成,包含{0}的字典的鍵為:",?words);

foreach?(var?keyValuePair?in?resultItem)

{

Console.WriteLine(keyValuePair.Key);

}

}

else

{

Console.WriteLine("查找完成,字典的值中不包含:{0}",?words);

}

Console.ReadLine();