Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从一个开关箱移动到另一个开关箱_C#_.net_Switch Statement - Fatal编程技术网

C# 从一个开关箱移动到另一个开关箱

C# 从一个开关箱移动到另一个开关箱,c#,.net,switch-statement,C#,.net,Switch Statement,我想从一个开关箱换到另一个: Switch() { } c=console.readline(); switch(yup) { case y: "I WANT TO CALL SWITCH 1;" case n: EXIT; } 我想这就是你想要的:- switch(yup) { case y: { //call your switch inside case y switch() { ----

我想从一个开关箱换到另一个:

Switch()
{ 

}
c=console.readline();
switch(yup)
{
   case y:
   "I WANT TO CALL SWITCH 1;"
   case n:
   EXIT;
}

我想这就是你想要的:-

switch(yup)
{
   case y:
   {
       //call your switch inside case y
       switch()
       {
         ----
         ----
       }
   }

   case n:
   EXIT;
}

要真正做到你所要求的,你需要一个
goto
。然而,大多数人都非常不喜欢
goto
,因为它使您的代码很难理解、调试和维护

通常,我会在循环时使用
do
/
,检查准备退出时设置的变量。大概是这样的:

bool done = false;

do
{
    switch(/* ... */)
    {
        // ...
    }

    c = Console.ReadLine();

    switch(yup)
    {
        case y:
            break;
        case n:
            done = true;
            break;
    }
}
while(!done);
如果你想“调用另一个开关”,你必须将它提取到一个单独的方法中,然后调用它

public static void Main()
{
    MethodWithFirstSwitch();        

    c=console.readline();

    switch(yup)
    {
        case y:
        MethodWithFirstSwitch();
        case n:
        EXIT;
    }
}

private static void MethodWithFirstSwitch()
{
     switch(something)
     {
          case "something":
          break;
          default:
          break;
     }
}
我不确定这是否是对你问题的准确估计。 如果您有一个开关,那么总是希望运行一些代码,然后是一个新开关,其中一个案例运行的开关与以前相同

由于不存在变量和退出
EXIT,这当然不会按原样运行调用,但作为示例


如果这不能回答您的问题,请更新原始帖子,提供您想要实现的目标的更多细节。

您能尝试以更好的方式解释您的问题吗?
退出
?是你发明的吗?
 private void Form1_Load(object sender, EventArgs e)
    {
        MyFirstSwitch(true);
        //c=console.readline();
        string yup;
        switch (yup)
        {
            case "y":
                MyFirstSwitch(false);
                break;
            case "n":
                //Exit
                break;
        }
    }

    private void MyFirstSwitch(bool check)
    {
        switch (check)
        {
            case true:
                //Do some thing hereZ
                break;

            case false:
                //Do some thing hereZ
                break;
        }
    }