【问题标题】:How to get a short data type value as input如何获取短数据类型值作为输入
【发布时间】:2012-10-20 14:14:00
【问题描述】:

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

【问题讨论】:

  • 我很想知道你到目前为止实际尝试了什么。
  • 我试图看到一个类似于ToInt16,ToDouble,ToString的函数。但是我找不到它。所以我提出了一个要求。
  • shortInt16 的别名。大多数基本类型都有简写别名以便于使用:msdn.microsoft.com/en-us/library/aa287910%28v=vs.71%29.aspx

标签: c# input short


【解决方案1】:
string input = Console.ReadLine();
short s;
if(short.TryParse(input, out s))
{
    //use s
}
else
{
    //invalid input
}

【讨论】:

  • 感谢您的回答。有一个清晰易懂的答案真是太好了。
【解决方案2】:
Var Input = Console.ReadLine();
Short ShortInput;
Int16.TryParse(Input,out ShortInput);

【讨论】:

  • 感谢您的回复。这是在 Visual Basic 中吗?
【解决方案3】:

您可以使用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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 2022-01-22
  • 2014-03-04
  • 1970-01-01
  • 2015-06-02
  • 2014-05-25
相关资源
最近更新 更多