1

我有点卡在这里。虽然我知道如何在 C# 中将键盘输入字符转换为可用字符。

char ch = (char)console.read();

我想读取数字并使它们在我的程序中表现为数字。

如果我从键盘输入 5,我想存储为 5(数学)而不是字符 5。

4

5 回答 5

1

最安全的方法是这样的;

int Num = 0;

if( int.TryParse( ch.ToString(), out Num ) )
{
   // Num is now set correctly
}
else
{
   // ch didn't contain a digit.
}
于 2012-09-06T17:00:47.880 回答
1

我想你想要这样的东西:

int Number;
string strNumber;

strNumber = Console.ReadLine();
Number = int.Parse(strNumber);
于 2012-09-06T16:55:23.050 回答
1

int i = ch - '0';////…………

于 2012-09-06T16:57:06.853 回答
0

试试 Convert.ToInt32("5"),一个安全的选择是 Int32.TryParse() 这里是文档:

http://msdn.microsoft.com/en-us/library/system.convert_methods

http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

于 2012-09-06T16:54:54.037 回答
0
string a = Console.ReadLine();
int b = int.Parse(a);
Console.WriteLine(b*b);
于 2012-09-06T16:58:57.073 回答