日期:2014-05-18  浏览次数:20961 次

怎么样设计一个在1到10中的计数器,当数为1时值为一,以此类推
PHP code
$num = array(
    1 => '一',
    2 => '二',
    3 => '三',
    4 => '四',
    5 => '五',
    6 => '六',
    7 => '七',
    8 => '八',
    9 => '九',
);

这是一段php代码,C#类似的思路应该是怎么样呢?

------解决方案--------------------
你可以定义个字典类型
C# code

            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "一");
            dic.Add(2, "二");
            dic.Add(3, "三");
            dic.Add(4, "四");
            dic.Add(5, "五");
            dic.Add(6, "六");
            dic.Add(7, "七");
            dic.Add(8, "八");
            dic.Add(9, "九");
            dic.Add(10, "十");
通过
dic.TryGetValue 根据key值得到大写的数字

------解决方案--------------------
C# code
string s = @"    1 => '一',
    2 => '二',
    3 => '三',
    4 => '四',
    5 => '五',
    6 => '六',
    7 => '七',
    8 => '八',
    9 => '九',
";
int n = 3;
string result = s.Substring(s.Select((x, i) => new { x, i }).Where(x => x.x - '0' == n).First().i + 6, 1);

------解决方案--------------------
C# code

 .net 3.5


 private static string[] meta = new string[] {"一", "二","三","四","五","六","七","八","九","十"};

        public static string Getxxxxxxxx(this int xI,int i)
        {
          if(Enumerable.Range(0,10).Contains(i))
          {
              return meta[i];
          }
            return string.Empty;
        }