连续输入以切换大小写,直到我按退出C# [英] Continuous input to switch case until i press exit c#

查看:106
本文介绍了连续输入以切换大小写,直到我按退出C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,在该程序中,除非按下选项'6',否则应用程序永远不要退出.下面是我的示例程序:现在,如果我按任何选项,它将执行相应的方法,并且控制台窗口在完成执行后关闭(退出应用程序).我希望控制台等待输入下一个选项.

I am trying to create a program where the application should never exit until I press option '6'. Below is my sample program: Right now, if I press any option, it executes the corresponding method and console window closes (exits the application) after it is done executing. I want the console to wait for the next option to be entered.

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

推荐答案

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

这篇关于连续输入以切换大小写,直到我按退出C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆