如何添加值不是数字的条件

时间:2016-05-02 07:37:11

标签: c#

就像现在一样,我只添加一个小于0的附加项,并且我想再添加一个条件,如果用户输入的值不是数字,仍会显示相同的消息框。

['19.18.3.0/20 [110/128] via 19.18.2.2, 00:00:50, Serial1/1', '12.18.3.1/20 [110/128] via 19.18.2.2, 00:00:50, Serial1/1', '12.18.1.0/20 [110/128] via 19.18.2.2, 00:00:50, Serial0/1']

1 个答案:

答案 0 :(得分:-1)

您可以执行以下操作:

 String input = Console.ReadLine() //Or whatever you are using to catch the user input
int n;
    if(int.TryParse(input, out n)
    {
         //Your logic, use n or something
    }
    else
    {
         //print error that there is no numeric input
    }