1

我正在尝试创建一个程序,在我按下选项“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;
           }
4

5 回答 5

1

您可以将代码放入循环中,然后更改returntobreak语句:

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

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

于 2013-06-20T20:23:20.093 回答
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;
           }
       }
于 2013-06-21T14:02:20.693 回答
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;
            }
        }
    }
}
于 2013-06-21T02:07:00.173 回答
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;
           }
}
于 2013-06-20T20:23:28.060 回答
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();
于 2013-06-20T20:23:46.047 回答