這僅僅是一個「最佳實踐」的問題...評估一系列條件和停止條件時遇到
我有一個功能,其採用輸入字符串,然後具有基於內容,但一旦改變它滿足特定的條件,則所有進一步處理停止。
目前,我使用的是「而(真)」循環,然後「破發」時,我已經得到了我想要的東西,下面是僞代碼..
string Input = "xyz";
string Output = string.Empty;
while (true)
{
if (Input.StartsWith("x"))
{
Output = "string starts with an X";
break;
}
if (Input.Contains("y"))
{
Output = "string has a 'y' in it";
break;
}
if (Input.IndexOf("z") == 2)
{
Output = "string has a 'z' as the 3rd character";
break;
}
Output = "string does not match any conditions";
break;
}
有沒有更「純粹主義」的方式達到上述目的?
感謝
[CodeReview.SE]會更好嗎? – 2013-04-23 08:12:52
在我以前用CA-Clipper編碼的美好時代,有BEGIN SEQUENCE .... END SEQUENCE的代碼構造,你可以在...之間「打破」... http://www.itlnet .net/programming/program/reference/c53g01c/ngfc7b7.html – 2013-04-23 09:21:18