char[] chArray = new char[] {'零','一','二','三','四','五'};
            Dictionary<char, char> dic = new Dictionary<char, char>();
            for (int i = 0; i <chArray.Length; i++)
            {
                dic.Add(Convert.ToChar(i),chArray[i]);
            }
            Console.WriteLine("请输入数字0-5");
            char num = Convert.ToChar(Console.ReadLine().Trim());
            Console.WriteLine(dic[num]);
            Console.ReadLine();为什么不可这样做呢?可以解释下吗?谢谢!dictionary

解决方案 »

  1.   

     char num = Convert.ToChar(Convert.ToInt32(Console.ReadLine().Trim()));
      

  2.   

    我就是这里不懂
    dic.Add(Convert.ToChar(i),chArray[i]);int转为char
    char num = Convert.ToChar(Console.ReadLine().Trim());shring转为char
    是编码不一样问题吗?我这里真不懂,虽然知道他们不一样。但还是不懂!
      

  3.   

                    dic.Add(Convert.ToChar(i+48), chArray[i]);
      

  4.   


            //
            // 摘要:
            //     将指定字符串的第一个字符转换为 Unicode 字符。
            //
            // 参数:
            //   value:
            //     长度为 1 的字符串。
            //
            // 返回结果:
            //     与 value 中第一个且仅有的字符等效的 Unicode 字符。
            //
            // 异常:
            //   System.ArgumentNullException:
            //     value 为 null。
            //
            //   System.FormatException:
            //     value 的长度不是 1。
            public static char ToChar(string value);  //
            // 摘要:
            //     将指定的 32 位有符号整数的值转换为它的等效 Unicode 字符。
            //
            // 参数:
            //   value:
            //     要转换的 32 位带符号整数。
            //
            // 返回结果:
            //     一个等于 value 的 Unicode 字符。
            //
            // 异常:
            //   System.OverflowException:
            //     value 小于 System.Char.MinValue 或大于 System.Char.MaxValue。
    看看这个你应该就明白了
      

  5.   

    Convert.ToChar(string value);这个重载只取第一个字符 作为char,
    Convert.ToChar(int32 value); 这个是一个等于 value 的 Unicode 字符。
    char a = '1';
    char b = (char)1;
    a和b当然是不一样的了
      

  6.   

    不是都是转换为Unicode 字符吗?
      

  7.   

    Console.ReadLine().Trim() 出来的是string类型,要先转换为int型
    char num = Convert.ToChar(Convert.ToInt32(Console.ReadLine().Trim()));
      

  8.   

    已经很明白了,关健字不存在的,Convert.ToChar转换的问题Convert.ToChar(i) //不会像你想像转换成 char i 
    Convert.ToChar(66) //只会转换在B要想正确执行还是把Dictionary<char,char> => 
    Dictionary<int, char> dic = new Dictionary<int, char>();