1. 程式人生 > >c#迴圈輸入的幾種解決方案

c#迴圈輸入的幾種解決方案

刷題的時候總會遇到那種多個測例,需要迴圈輸入,現總結幾種常用並簡潔優(cu)雅(bao)的方法。
1.適合空白字元輸入後進行操作會報錯的,比如有string轉int操作的

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

2.適合確定輸入空白字元後進行操作不會報錯的

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