Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将键盘输入数字转换为数字类型_C#_Asp.net - Fatal编程技术网

C# 将键盘输入数字转换为数字类型

C# 将键盘输入数字转换为数字类型,c#,asp.net,C#,Asp.net,我有点困在这里了。虽然我知道如何将键盘输入字符转换成C#中可用的字符 我想读取数字,并使它们在我的程序中表现为数字 如果我从键盘输入5,我想将其存储为5(数学)而不是字符5。尝试转换.ToInt32(“5”),一个安全的替代方法是Int32.TryParse() 我想你想要这样的东西: int Number; string strNumber; strNumber = Console.ReadLine(); Number = int.Parse(strNumber); inti=ch-'0

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

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

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

尝试转换.ToInt32(“5”),一个安全的替代方法是Int32.TryParse()


我想你想要这样的东西:

int Number;
string strNumber;

strNumber = Console.ReadLine();
Number = int.Parse(strNumber);

inti=ch-'0'
string a = Console.ReadLine();
int b = int.Parse(a);
Console.WriteLine(b*b);
int Num = 0;

if( int.TryParse( ch.ToString(), out Num ) )
{
   // Num is now set correctly
}
else
{
   // ch didn't contain a digit.
}

+1:很好用…对于通常的十进制数字。我相信Unicode分类号、十进制数字比通常的0-9包含更多的选项:这种方式很好,只要确保
ch>=“0”&&ch这是一个ASP.NET问题吗?您在ASP.NET中没有控制台。是的,确切地说,这就是我问题的答案。
int Num = 0;

if( int.TryParse( ch.ToString(), out Num ) )
{
   // Num is now set correctly
}
else
{
   // ch didn't contain a digit.
}