循环switch语句 [英] Looping a switch statement

查看:131
本文介绍了循环switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#。我有一个控制台应用程序一个菜单。现在,当我已经完成了挑选一个项目出菜单的,做什么菜单项要求我要循环,并再次显示菜单,以便用户可以选择一个diffenrent菜单项。我有菜单上的退出,我只是想用它来退出。我已经尝试了while循环但这并不工作。它关闭应用程序的菜单项已被选中,被选中的项目的代码运行之后。我在做什么错

I am new to C#. I have a "menu" in a console app. Now when I have finished picking an item out of the menu and doing what that menu item require I want to loop and show the menu again so that the user can choose a diffenrent menu item. I have an exit on the menu and I only want to use that to exit. I have tried a while loop but this doesnt work. It closes the app after a menu item has been chosen and the chosen items code has run. What am I doing wrong?

static void Main()
    {   
        int input = 0;
        while (true)
        {

            Console.WriteLine("MENU");
            Console.WriteLine("Please enter the number that you want to do:");
            Console.WriteLine("1. Do thing A");
            Console.WriteLine("2. Do thing B");
            Console.WriteLine("3. Do thing C");
            Console.WriteLine("4. Do thing D");
            Console.WriteLine("5. Do thing E");
            Console.WriteLine("6. Do thing F");
            Console.WriteLine("7. Exit");

            int menuchoice = int.Parse(Console.ReadLine());

            switch (menuchoice)
            {
                case 1:
                    Console.WriteLine("Thing A has been done"); 
                    break;
                case 2:
                    Console.WriteLine("Thing B has been done");
                    break;
                case 3:
                    Console.WriteLine("Thing C has been done");
                    break;
                case 4:
                    Console.WriteLine("Thing D has been done");
                    break;
                case 5:
                    Console.WriteLine("Thing E has been done");
                    break;
                case 6:
                    Console.WriteLine("Thing F has been done");
                    break;
                case 7:
                    Environment.Exit; //edit
                    break;
                default:
                    Console.WriteLine("Sorry, invalid selection");
                    break;
            }

            input++;
            if (input < 30)
                continue;
            else
                break;
        }           
    }



任何人都可以请帮助?在此先感谢

Can anyone please help? Thanks in advance!

编辑:!我知道了Console.Exit是行不通的。我也只是把它像那样illastrate,控制台必须退出那里。我的问题是我需要循环整个菜单每次选项被选中,被选中的选项代码运行之后。我只想使用exit退出。但在这一点上,菜单不循环,经过​​短短1选项被选中,并且选择代码运行控制台关闭

I am aware that that "Console.Exit" would not work. I did just put it like that to illastrate that the console must exit there. My problem is that I need to loop the whole menu every time after a option have been chosen and the chosen option code has run. I only want to use the exit to exit. But at this point the menu does not loop, the console closes after just 1 option have been chosen and that options code has run.

编辑:会发生什么事,当您启动程序,并按下1后的回报?这是真正的问题,菜单似乎不进行循环。我开始我的程序后,按1依次是返回的情况下,代码1运行完美,但随后控制台只是关闭。如果我再并按启动控制台2这段时间的情况下,2的代码也运行完美,但随后控制台再次关闭。我测试了我所有的情况下,像这样和他们都运行完美。

What happens when you start your program and you press 1 followed by Return? This is the real problem, the menu doesnt seem to be looping. After I start my program and press 1 followed by return the code in case 1 runs perfectly but then the console just closes. If I start the console again and press 2 this time the code in case 2 also runs perfectly but then the console closes again. I have tested all my cases like this and all of them runs perfectly.

推荐答案

请简单:直到按下7循环停留在循环

Keep it simple: the loop stays in the loop until 7 is pressed

  int menuchoice  = 0;  
        while (menuchoice != 7)  
        {  

            Console.WriteLine("MENU");  
            Console.WriteLine("Please enter the number that you want to do:");  
            Console.WriteLine("1. Do thing A");  
            Console.WriteLine("2. Do thing B");  
            Console.WriteLine("3. Do thing C");  
            Console.WriteLine("4. Do thing D");  
            Console.WriteLine("5. Do thing E");  
            Console.WriteLine("6. Do thing F");  
            Console.WriteLine("7. Exit");  

            menuchoice = int.Parse(Console.ReadLine());  

            switch (menuchoice)  
            {  
                case 1:  
                    Console.WriteLine("Thing A has been done");   
                    break;  
                case 2:  
                    Console.WriteLine("Thing B has been done");  
                    break;  
                case 3:  
                    Console.WriteLine("Thing C has been done");  
                    break;  
                case 4:  
                    Console.WriteLine("Thing D has been done");  
                    break;  
                case 5:  
                    Console.WriteLine("Thing E has been done");  
                    break;  
                case 6:  
                    Console.WriteLine("Thing F has been done");  
                    break;  
                case 7:  
                    break;  //edit
                default:  
                    Console.WriteLine("Sorry, invalid selection");  
                    break;  
            }  

这篇关于循环switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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