我在C#控制台应用程序中声明了字符数组。
我的代码:
char[] address = new char[30];
char[] blood_Grp = new char[10];
public void getdata()
{
Console.WriteLine("enter your address");
// here I am getting FormatException was unhandled exception
address[i] = Convert.ToChar(Console.ReadLine());
}
请帮我治好我的编码..
答案 0 :(得分:2)
问题在于您正在阅读一个完整的行(chars
数组)。如果输入的长度大于1,则将抛出FormatException
。
您应该使用Console.ReadKey()
。
address[i] = Console.ReadKey().KeyChar;
它有一个属性KeyChar
,因此您不需要自己将其转换为char
。
答案 1 :(得分:0)
Console.ReadLine将返回字符串
然后您可以使用String.ToCharArray来获取字符
答案 2 :(得分:0)
尝试
string address = Console.ReadLine();
...或
char[] address = Console.ReadLine().ToCharArray();