连续输入到切换器的情况下,直到我按下退出C#(Continuous input to switch

2019-10-18 06:34发布

我想创建一个程序,该应用程序应该永远不会退出,直到我按选项“6”。 下面是我的示例程序:现在,如果我按任何选项,则执行相应的方法和控制台窗口关闭(退出应用程序),它执行完毕后。 我希望控制台等待进入下一个选项。

Console.WriteLine(@"Select one of the following option:                                
                            1-Write Apps 
                            2-Write Drivers
                            3-Write OS
                            4-Write Packages
                            5-All the above
                            6-Exit");
           string strReadKey = Console.ReadKey().KeyChar.ToString();

           int.TryParse(strReadKey, out selectionKey);


           switch (selectionKey)
           {

               case 1:
                   DoApps(reqObj);
                   return;
               case 2:
                   DoDrivers(reqObj);
                   return;
               case 3:
                   DoOS(reqObj);
                   return;
               case 4:
                   DoPackages(reqObj);
                   return;
               case 5:
                   DoAll(reqObj);
                   return;
               case 6:
                   Environment.Exit(0);                       
                   return;
               default:
                   DoAll(reqObj);
                   return;
           }

Answer 1:

using System;

namespace LoopUntilSelectionTester
{
    class Program
    {
        static void Main()
        {            
            string key;
            while((key = Console.ReadKey().KeyChar.ToString()) != "6")            
            {
                int keyValue;                
                int.TryParse(key, out keyValue);

                ProcessInput(keyValue);
            }    
        }

        private static void ProcessInput(int keyValue)
        {
            switch (keyValue)
            {
                case 1:
                    DoApps(reqObj);
                    break;
                case 2:
                    DoDrivers(reqObj);
                    break;
                case 3:
                    DoOS(reqObj);
                    break;
                case 4:
                    DoPackages(reqObj);
                    break;
                case 5:
                    DoAll(reqObj);
                    break;
            }
        }
    }
}


Answer 2:

你只需把你的代码放到一个循环,并更改returnbreak语句:

while(true) // Loop forever
{
      string strReadKey = Console.ReadKey().KeyChar.ToString();
      int.TryParse(strReadKey, out selectionKey);


       switch (selectionKey)
       {

           case 1:
               DoApps(reqObj);
               break; // Break, don't return
           case 2:
               DoDrivers(reqObj);
               break;
           case 3:
               DoOS(reqObj);
               break;
           case 4:
               DoPackages(reqObj);
               break;
           case 5:
               DoAll(reqObj);
               break;
           case 6:
               Environment.Exit(0);                       
               break;
           default:
               DoAll(reqObj);
               break;
       }
}

如果您想提示输入每个迭代,你可以移动迅速进入死循环,也是如此。



Answer 3:

首先,我会做的是使之成为一个方法:

public void AskForInput()
{
Console.WriteLine(@"Select one of the following options:                                
                        1-Write Apps 
                        2-Write Drivers
                        3-Write OS
                        4-Write Packages
                        5-All the above
                        6-Exit");
       string strReadKey = Console.ReadKey().KeyChar.ToString();
       int.TryParse(strReadKey, out selectionKey);
       switch (selectionKey)
       {
           case 1:
               DoApps(reqObj);
               return;
           case 2:
               DoDrivers(reqObj);
               return;
           case 3:
               DoOS(reqObj);
               return;
           case 4:
               DoPackages(reqObj);
               return;
           case 5:
               DoAll(reqObj);
               return;
           case 6:
               Environment.Exit(0);                       
               return;
           default:
               DoAll(reqObj);
               return;
       }
}

然后就用这样的:

for (;;) AskForInput();


Answer 4:

只是包装整个事情while语句。

while (true)
{
     Console.WriteLine(@"Select one of the following option:                                
                            1-Write Apps 
                            2-Write Drivers
                            3-Write OS
                            4-Write Packages
                            5-All the above
                            6-Exit");
           string strReadKey = Console.ReadKey().KeyChar.ToString();

           int.TryParse(strReadKey, out selectionKey);


           switch (selectionKey)
           {

               case 1:
                   DoApps(reqObj);
                   break;
               case 2:
                   DoDrivers(reqObj);
                   break;
               case 3:
                   DoOS(reqObj);
                   break;
               case 4:
                   DoPackages(reqObj);
                   break;
               case 5:
                   DoAll(reqObj);
                   break;
               case 6:
                   Environment.Exit(0);                       
                   break;
               default:
                   DoAll(reqObj);
                   break;
           }
}


Answer 5:

这为我工作:

 Console.WriteLine(@"Select one of the following option:                                
                            1-Write Applications XML
                            2-Write Drivers XML
                            3-Write Operating Systems XML
                            4-Write Packages XML
                            5-All the above
                            6-Exit");
           string strReadKey = Console.ReadKey().KeyChar.ToString();

           int.TryParse(strReadKey, out selectionKey);
           while (true)
           {

           switch (selectionKey)
           {                       
               case 1:
                   DoApplications(reqObj);
                   strReadKey = Console.ReadKey().KeyChar.ToString();
                   int.TryParse(strReadKey, out selectionKey);
                   break;
               case 2:
                   DoDrivers(reqObj);
                   strReadKey = Console.ReadKey().KeyChar.ToString();
                   int.TryParse(strReadKey, out selectionKey);
                   break;
               case 3:
                   DoOperatingSystems(reqObj);
                   strReadKey = Console.ReadKey().KeyChar.ToString();
                   int.TryParse(strReadKey, out selectionKey);
                   break;
               case 4:
                   DoPackages(reqObj);
                   strReadKey = Console.ReadKey().KeyChar.ToString();
                   int.TryParse(strReadKey, out selectionKey);
                   break;
               case 5:
                   DoAll(reqObj);
                   strReadKey = Console.ReadKey().KeyChar.ToString();
                   int.TryParse(strReadKey, out selectionKey);
                   break;
               case 6:
                   Environment.Exit(0);
                   break;
               default:
                   DoAll(reqObj);
                   strReadKey = Console.ReadKey().KeyChar.ToString();
                   int.TryParse(strReadKey, out selectionKey);
                   break;
           }
       }


文章来源: Continuous input to switch case until i press exit c#