切换,一个声明后停止案件 [英] switch, case stop after one statement

查看:56
本文介绍了切换,一个声明后停止案件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:



I have this:

static void Main(string[] args)
       {
           //TimePlanner timePlanner = new TimePlanner();
           //LoginDetails loginDetails = new LoginDetails();

           IBotAction botAction;
           //string value;
           Console.WriteLine("choose the option you want to see");
           Console.WriteLine("\n1.Type timePlanner - here you can see your tasks ");
           Console.WriteLine("\n2.Type  loginDetails - Here you can see the log in data of the client");
           Console.WriteLine("\nType here your choice");
           string input = Console.ReadLine();
           while (true)
           {
               //if (input == "exit")
               //{


                   switch (input)
                   {
                       case "timePlanner":
                           // Console.WriteLine("You have choosen for Time Planner");
                           botAction = new TimePlanner();
                           botAction.ActionName();
                           break;
                       case "loginDetails":
                           //Console.WriteLine("You have choosen for Log in Details");
                           botAction = new LoginDetails();
                           botAction.ActionName();
                           break;


                       default:
                           break;
                   }
               //}
               //break;


           }


           Console.ReadKey();
       }





但问题是输出字符串继续,并且在第一次输出后没有停止



谢谢



but the problem is that the output string continued, and doesnt stop after first output

Thank you

推荐答案

你有一个永无止境的循环

You have a never ending loop
while (true)
...



这将导致程序一次又一次地一次又一次地重复切换语句......


This will cause the program to repeat the switch statement again and again and again and again and again and again and again...


根据我的评论,你不是突破(无限)循环。



尝试这样的事情:
As per my comment, you are not breaking out of that (infinite) while loop.

Try something like this instead:
Console.WriteLine("\nType here your choice");
Console.WriteLine("\nOr type 'Exit' to end");
while (true)
{
    string input = Console.ReadLine();
    if (input.ToLower() == "exit")
    {
        break;
    }
    switch (input)
etc...


像这样:



Like this:

hile (true)
            {
                //if (input == "exit")
                //{


                    switch (input)
                    {
                        case "timePlanner":
                            // Console.WriteLine("You have choosen for Time Planner");
                            botAction = new TimePlanner();
                            botAction.ActionName();
                            break;
                        case "loginDetails":
                            //Console.WriteLine("You have choosen for Log in Details");
                            botAction = new LoginDetails();
                            botAction.ActionName();
                            break;
                        default:
                            break;
                    }
                    break;
                //}
                //break;


            }



            Console.ReadKey();
        }







我现在有这样的:






I have it now like this:

while (true)
           {
               //if (input == "exit")
               //{
               string input = Console.ReadLine();
               if (input.ToLower() == "exit")
               {
                   break;
               }

                   switch (input)
                   {
                       case "timePlanner":
                           // Console.WriteLine("You have choosen for Time Planner");
                           botAction = new TimePlanner();
                           botAction.ActionName();
                           break;
                       case "loginDetails":
                           //Console.WriteLine("You have choosen for Log in Details");
                           botAction = new LoginDetails();
                           botAction.ActionName();
                           continue;
                       case "Q":
                           break;
                       default:
                           break;
                   }
                   break;
               //}
               //break;


           }



           Console.ReadKey();
       }





但问题是我只能输入一个选项



but the problem is that I only can type one option


这篇关于切换,一个声明后停止案件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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