C# Dictionary.ContainsValue()始终返回true
我有一本字典,里面有所有映射到摩尔斯电码的字母C# Dictionary.ContainsValue()始终返回true,c#,.net,dictionary,c#-4.0,C#,.net,Dictionary,C# 4.0,我有一本字典,里面有所有映射到摩尔斯电码的字母 Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("a", ".-"); data.Add("b", "-..."); data.Add("c", "-.-."); data.Add("d", "-.."); data.Add("e", "."
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("a", ".-");
data.Add("b", "-...");
data.Add("c", "-.-.");
data.Add("d", "-..");
data.Add("e", ".");
data.Add("f", "..-.");
data.Add("g", "--.");
data.Add("h", "....");
data.Add("i", "..");
data.Add("j", ".---");
data.Add("k", "-.-");
data.Add("l", ".-..");
data.Add("m", "--");
data.Add("n", "-.");
data.Add("o", "---"); and so on..
字典数据=新字典();
数据。添加(“a”、“-”);
数据。添加(“b”和“-…”);
数据。添加(“c”和“-.-”;
数据。添加(“d”和“-……”);
数据。添加(“e”和“);
数据。添加(“f”和“.-”);
数据。添加(“g”和“--”);
数据。添加(“h”、“…”);
数据。添加(“i”、“.”);
数据。添加(“j”、“--”);
数据。添加(“k”和“-.-”;
数据。添加(“l”和“-”);
数据。添加(“m”和“--”);
数据。添加(“n”和“-”);
数据。添加(“o”和“---”);等等
我试图检查字典中是否存在现有莫尔斯电码的子串
foreach (var item in arraylist)
{
int smallcount=0;
int startIndex = 0;
//check if this combination exists for morse code
for(int w=0;w<shortInput;w++)
{
int substringLength=Convert.ToInt32(item[w].ToString());
string sub = morsecode.Substring(startIndex, substringLength);
if (data.ContainsValue(sub)) ;
{
smallcount++;
}
startIndex = startIndex + substringLength;
}
if(smallcount==shortInput)
{ count++; }
}
foreach(arraylist中的变量项)
{
int smallcount=0;
int startIndex=0;
//检查摩尔斯电码是否存在此组合
for(int w=0;wContainsValue
实际上并没有返回true,但是if语句后面有一个零散的分号。这意味着将始终执行以下块,因为它不是有条件地执行的。它的处理方式如下:
if (data.ContainsValue(sub))
{
}
{
smallcount++;
}
相反,请删除分号,以便实际上在if语句后面有一个块,如下所示:
if (data.ContaisnValue(sub))
{
smallcount++;
}