连续输入以切换外壳直到我按下退出c#

时间:2013-06-20 20:21:23

标签: c# input switch-statement case continuous

我正在尝试创建一个程序,应用程序永远不会退出,直到我按下选项'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;
           }

5 个答案:

答案 0 :(得分:1)

您可以将代码放入循环中,并将return更改为break语句:

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;
       }
}

如果您希望每次迭代都提示输入,您也可以将提示移动到循环中。

答案 1 :(得分:0)

将整个内容包装在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;
           }
}

答案 2 :(得分:0)

首先,我要做的是将其变成一种方法:

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();

答案 3 :(得分:0)

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;
            }
        }
    }
}

答案 4 :(得分:0)

这对我有用:

 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;
           }
       }