2014-10-19 77 views
-3

我有這樣的問題:無法到達的代碼檢測到的字符串Speech = e.Result.Text;

http://s28.postimg.org/x8gb3pvkb/Capture.png 這是谷歌的搜索代碼通過語音:

string Speech = e.Result.Text; 
if (Speech == "I WANT TO SEARCH SOMETHING") 
    { 
    QEvent = Speech; 
    JARVIS.SpeakAsync("what do you want to search"); 
    Speech = string.Empty; 
    } 
else if (Speech != string.Empty && QEvent == "I WANT TO SEARCH SOMETHING") 
    { 
     System.Diagnostics.Process.Start("http://google.com/search?q=" + Speech); 
    QEvent = string.Empty; 

    ranNum = rnd.Next(1, 4); 
    if (ranNum == 1) { JARVIS.SpeakAsync("Alright, I am searching " + Speech + " in google"); } 
    else if (ranNum == 2) { JARVIS.SpeakAsync("ok sir, I am searching " + Speech); } 
    else if (ranNum == 3) { JARVIS.SpeakAsync("Alright, I am searching "); } 
    Speech = string.Empty; 

    } 

而且在錯誤列表中顯示我這個錯誤:

Unreachable code Detected 

你能告訴我爲什麼這個是無法訪問的代碼?

+1

什麼行是錯誤? – box86rowh 2014-10-19 13:30:35

+1

啊,看到了截圖,你必須在你粘貼的代碼之前關閉開關。 – box86rowh 2014-10-19 13:32:13

+2

你忘了用'}關閉你的'switch' – Rotem 2014-10-19 13:32:22

回答

0

您需要關閉開關

switch (caseSwitch) 
{ 
    case "open": 
     SendKeys.Send("{ENTER}"); 
     break; 
    default: 
     //do something 
     break; 
} 
string Speech = e.Result.Text; 
0

我認爲這個錯誤是因爲你沒有關閉您的switch語句,你表現出你的形象。由於這是由於swithc發生的,所以您發佈的代碼是不相關的。

您在您的case: "Open"中使用了break;,您的string Speech = e.Result.Text;行無法訪問。

From documentation;

Execution of the statement list in the selected switch section begins with the first statement and proceeds through the statement list, typically until a jump statement, such as a break, goto case, return, or throw, is reached. At that point, control is transferred outside the switch statement or to another case label.

switch (expr) 
{ 
    ... 
    case "Open": 
     SendKeys.Send("{ENTER}"); 
     break; 
} 
string Speech = e.Result.Text; 
0

從截圖,它表明你已經把代碼break後,所以這些線路將不會被執行,因爲,後break將跳過所有的代碼,這就是爲什麼它顯示Unreachable code

因此,無論您需要將break放在那些代碼塊之後或刪除那些代碼塊並放在switch-case之後。

相關問題