2016-03-01 87 views
0

我通常从不使用字符,所以很抱歉,如果它似乎是一个noob问题。我做了Crypter,并希望做“如果有至少4型动物字符,然后隐窝,否则不地下室和返回错误”计算多少个字符

实际代码:

string msg = Console.ReadLine(); 
char[] msgToChar = msg.ToCharArray(); 
if(here is the problem) 
{ 
Console.WriteLine("Crypted message: " + Crypt(msg.ToUpper())); 
} 
else 
{ 
Console.WriteLine("Please enter at least 4 differentes characters."); 
} 
+2

你可以使用'GroupBy'。 –

+1

'Distinct'和'Count'会更容易阅读。 – CaffGeek

回答

14

你可以这样做得到不同的字符数。

msgToChar.Distinct().Count() 

所以,你如果会像

if (msgToChar.Distinct().Count() >= 4) 
相关问题