C# 中的while循环输入

适合空白字符输入后进行操作会报错的,比如有string转int操作的

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    var next = Console.ReadLine();
                    //other process code
                }
                catch
                {
                    break;
                }
            }
        }
    }
}

适合确定输入空白字符后进行操作不会报错的

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var next = Console.ReadLine();
            while (!string.IsNullOrWhiteSpace(next))
            {
                //process code
                next = Console.ReadLine();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44054505/article/details/101061144