C# How to loop user input until the datatype of the input is correct?

前端 未结 4 1538
春和景丽
春和景丽 2020-12-03 16:34

How to make this piece of code loop asking for input from the user until int.TryParse()

is successful?

//setX
    public void setX()
    {
          


        
4条回答
  •  无人及你
    2020-12-03 17:15

    This can help too

    public int fun()
    {
        int Choice=0;
        try 
        {
            Choice = int.Parse(Console.ReadLine());
            return choice;
        } 
        catch (Exception) 
        {
            return fun(); 
        }
    }
    

提交回复
热议问题