2012-10-20 66 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

不,這是犀利的 –