當前位置:成語大全網 - 新華字典 - .net :怎麽判斷數字的數位和某個數的個數C#

.net :怎麽判斷數字的數位和某個數的個數C#

字典來遍歷

Console.WriteLine("Please enter the number:");

string str = Console.ReadLine();

//判定輸入是否為壹個整數

int number = 0;

bool ISOK = Int32.TryParse(str, out number);

if (ISOK)

{

//用壹個字典對象來放查尋的關鍵字與出現次數

Dictionary<Char, Int32> arr = new Dictionary<char, int>();

var list = str.ToCharArray(); //把str轉成數組方便遍歷

foreach (var c in list)

{

if (arr.Count(a=>a.Key == c) == 0)

{

arr.Add(c, list.Count(x=>x == c));

}

}

//輸入

Console.WriteLine(str + ":" + str.Length + "bits");

foreach (var obj in arr)

{

if (obj.Value > 0)

{

Console.WriteLine(obj.Key + ":" + obj.Value);

}

}

}

else

{

Console.WriteLine("妳輸入的必須是整數");

}

Console.WriteLine("按任意鍵退出...");

Console.ReadKey();