2012-09-01 86 views
-3

我想從一個開關的情況下移動到另一個:移動到另一個

Switch() 
{ 

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

你能嘗試以更好的方式解釋你的問題嗎? –

+1

'exit'?你剛發明了那個嗎? – DarthVader

回答

1

如果你想「調用另一個開關」,你必須將它解壓縮到一個單獨的方法,並調用它。

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

我不確定這是否是您的問題的準確aproximation。 你有一個開關,然後總是想運行一些代碼,然後是一個新的開關,其中一個情況運行與之前相同的開關。

由於存在不存在的變量和EXIT;調用,因此這不會按照當然的方式運行,但可作爲示例。

如果這不能解決您的問題,請更新原始帖子,詳細說明您正在嘗試實現的內容。

0

我想這是你想要什麼: -

0

要做到從字面上你」再問你需要一個goto。然而,大多數人對goto非常不喜歡,因爲它會讓你的代碼很難理解,調試和維護。

通常我會使用do/while循環,檢查您在準備退出時設置的變量。像這樣:

bool done = false; 

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

    c = Console.ReadLine(); 

    switch(yup) 
    { 
     case y: 
      break; 
     case n: 
      done = true; 
      break; 
    } 
} 
while(!done); 
0
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; 
     } 
    }