如何从控制台读取char [英] How to read char from the console

查看:163
本文介绍了如何从控制台读取char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 char 数组,我想从控制台分配值。这是我的代码:

I have a char array and I want to assign values from the console. Here's my code:

char[] input = new char[n];
for (int i = 0; i < input.Length; i++)
{
    input[i] = Console.ReadLine();
}

但是我遇到了以下错误:

But I'm getting the following error:


不能将类型'System.ConsoleKeyInfo'隐式转换为'char'

Cannot implicitly convert type 'System.ConsoleKeyInfo' to 'char'

是否有简单的方法来解决此问题?

Is there an easy way to fix this?

推荐答案

使用 Console.ReadKey ,然后 KeyChar 来获取 char ,因为 ConsoleKeyInfo 不能分配给 char ,如您的错误所述。

Use Console.ReadKey and then KeyChar to get char, because ConsoleKeyInfo is not assignable to char as your error says.

input[i] = Console.ReadKey().KeyChar;

这篇关于如何从控制台读取char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆