有一數組,內存放Y,N共32個,我想寫一方法算出數組中Y的個數,謝謝

解决方案 »

  1.   

    我這寫了一個方法,不知道有沒有更好的, 
    public int getMonthworkday(string[] ckbdate) 
        {
            int i = 0;
            for (int j = 0; j < 32; j++ )
            {
                if (ckbdate[j] == "Y") { i++;}
            }
            return i;            
        }
      

  2.   

    char[] b = new char[4] { 'Y','N','N','N'};
                string tem = (new string(b)).Replace("Y", "");
                Console.WriteLine(tem.Length);
      

  3.   

    string[] str = new string[32] { "Y", "N", "N", "Y", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "N", "N", "Y"};
                Array.Sort(str);
                int length = str.GetLength(0) - Array.BinarySearch(str, "Y", null);
                MessageBox.Show(length.ToString()); ;
      

  4.   

    1:
    char[] teststr = {'Y','N','Y','N' };
    Response.Write(Regex.Matches(new string(teststr), @"Y").Count);2:
    循环哪个效率高些呢:)