2012-06-18 77 views
-3

我不知道爲什麼我得到這個以下錯誤:C#切換貫穿錯誤?

Control cannot fall through from one case label ('case "h":') to another (CS0163)

僅適用於H和S - 很奇怪。

switch(myChoice) 
{ 
    case "K": 
    case "k": 
     Console.WriteLine("You have chosen the Kanto region"); 
     break; 
    case "O": 
    case "o": 
     Console.WriteLine("You have chosen the Orange Islands"); 
     break; 
    case "J": 
    case "j": 
     Console.WriteLine("You have chosen the Johto region"); 
     break; 
    case "H": 
    case "h": 
     Console.WriteLine("You have chosen the Hoenn region"); 
    case "S": 
    case "s": 
     Console.WriteLine("You have chosen the Sinoh region"); 
    case "U": 
    case "u": 
     Console.WriteLine("You have chosen the Unova region"); 
     break; 
    case "R": 
    case "r": 
     Console.WriteLine("Return"); 
     break; 
    default: 
     Console.WriteLine("{0} is not a valid choice", myChoice); 
     break; 
} 
+2

只是站起來:你現在問了三個問題,現在有一個可以接受的答案。您應該通過點擊答案旁邊的複選標記來接受這些答案。 –

+0

哦天哪謝謝><,我從來不知道那個 –

回答

11

下通之間的break語句時,case語句沒有身體纔有效。由於您的「h」和「s」案件都有代碼,因此您需要後面的break

此外,作爲一個建議:你可以在你的switch參數做String.ToUpper()這樣你可以避免檢查小寫和大寫myChoice變種兩者。你switch聲明則變爲:

switch(myChoice.ToUpper()) 
{ 
    case "K": 
     Console.WriteLine("You have chosen the Kanto region"); 
     break; 
    case "O": 
     ... 
} 
+0

不一定。在這個地方應該有一個休息,返回或轉到。看到我的答案與goto的下面.. – theking2

10

你錯過了在S和H的情況下

1

break您忘記H和S.

0

你缺少

break;

聲明...

case "H": 
case "h": 
    Console.WriteLine("You have chosen the Hoenn region"); 
    break; 
case "S": 
case "s": 
    Console.WriteLine("You have chosen the Sinoh region"); 
break; 
0

後案 「H」 添加Goto字母 「h」 後案 「S」 添加Goto情況下, 「S」

case "H": 
    goto case "h"; 
case "h": 
    Console.WriteLine("You have chosen the Hoenn region"); 
    break; 
case "S": 
    goto case "s"; 
case "s": 
    Console.WriteLine("You have chosen the Sinoh region"); 
break; 

是的,C#的goto的回來了!