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
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
dic.Add(Convert.ToChar(i),chArray[i]);int转为char
char num = Convert.ToChar(Console.ReadLine().Trim());shring转为char
是编码不一样问题吗?我这里真不懂,虽然知道他们不一样。但还是不懂!
//
// 摘要:
// 将指定字符串的第一个字符转换为 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。
看看这个你应该就明白了
Convert.ToChar(int32 value); 这个是一个等于 value 的 Unicode 字符。
char a = '1';
char b = (char)1;
a和b当然是不一样的了
char num = Convert.ToChar(Convert.ToInt32(Console.ReadLine().Trim()));
Convert.ToChar(66) //只会转换在B要想正确执行还是把Dictionary<char,char> =>
Dictionary<int, char> dic = new Dictionary<int, char>();