2014-11-15 27 views
0

我已经在我的C#控制台应用程序中声明了字符数组。FormatException被未处理发生

我的代码:

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

请编辑您的帖子,并使用代码标记来格式化你的代码,同时也包括你在哪里得到异常的堆栈跟踪。 – mattias

回答

2

的问题是你正在阅读一个完整的行(一组chars)。如果输入的长度大于1,则会抛出FormatException。您可以使用Console.ReadKey()

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

它有一个属性KeyChar,所以你不需要自己动手转换为char

0

尝试

string address = Console.ReadLine(); 

或...

char[] address = Console.ReadLine().ToCharArray(); 
相关问题