Reading an integer from user input

后端 未结 11 798
时光取名叫无心
时光取名叫无心 2020-11-22 09:44

What I am looking for is how to read an integer that was given by the user from the command line (console project). I primarily know C++ and have started down the C# path. I

11条回答
  •  旧时难觅i
    2020-11-22 09:51

    Try this it will not throw exception and user can try again:

            Console.WriteLine("1. Add account.");
            Console.WriteLine("Enter choice: ");
            int choice = 0;
            while (!Int32.TryParse(Console.ReadLine(), out choice))
            {
                Console.WriteLine("Wrong input! Enter choice number again:");
            }
    

提交回复
热议问题