2012-10-20 27 views
0

我需要很短的数据类型值,如C#控制台应用程序一个输入。我听说,它很容易在C#中的int,string等输入。但是我必须得到一个简短的数据类型值作为输入。请帮帮我。 谢谢!如何获得短数据类型值作为输入

+3

我很好奇,想知道你实际上到目前为止已经试过。 –

+0

我想看到类似ToInt16,ToDouble,ToString.But我找不到它的简称功能。所以我提出了一个要求。 – Shriram

+0

'short'是'Int16'的别名。大部分的基本类型有易于使用的速记别名:http://msdn.microsoft.com/en-us/library/aa287910%28v=vs.71%29.aspx –

回答

0

您可以使用Console.ReadLine()并在其上应用检查,如果给定值不是短,那么你要求再次输入。

string inputForShort = Console.ReadLine(); 
short result=0; 
bool shortGiven = false; 
do { 
     Console.WriteLine("Please enter short value"); 
     inputForShort = Console.ReadLine(); 
     shortGiven = short.TryParse(inputForShort , out result);   

} while (!shortGiven); 

Console.WriteLine("You entered short value" + result); 
+0

感谢您的答复。它非常有用! – Shriram

+0

不客气,这是你的知识,因为你是新来的StackOverFlow,http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Adil

2
string input = Console.ReadLine(); 
short s; 
if(short.TryParse(input, out s)) 
{ 
    //use s 
} 
else 
{ 
    //invalid input 
} 
+0

感谢您的答案。真的很高兴有一个明确可以理解的答案。 – Shriram

0
Var Input = Console.ReadLine(); 
Short ShortInput; 
Int16.TryParse(Input,out ShortInput); 
+0

感谢您的回复。这是在Visual Basic中吗? – Shriram

+0

不,这是犀利的 –