我有一个char
数组,我想从控制台分配值。这是我的代码:
char[] input = new char[n];
for (int i = 0; i < input.Length; i++)
{
input[i] = Console.ReadLine();
}
但是我收到以下错误:
Cannot implicitly convert type 'System.ConsoleKeyInfo' to 'char'
有没有一种简单的方法来解决这个问题?
发布于 2018-07-31 12:36:29
使用Console.ReadKey
,然后用KeyChar
获取char
,因为ConsoleKeyInfo
不能像你的错误所说的那样分配。
input[i] = Console.ReadKey().KeyChar;
发布于 2018-07-31 13:38:14
public static void DoThis(int n)
{
var input = new char[n];
for (var i = 0; i < input.Length; i++)
{
input[i] = Console.ReadKey().KeyChar;
}
Console.WriteLine(); // Linebreak
Console.WriteLine(input);
Console.ReadKey();
}
https://stackoverflow.com/questions/-100001780
复制相似问题