2011-11-28 68 views
0

獲取每個特定數字。我可能會問一個簡單的問題。好吧,讓我開始吧。C#,再次從0到9999

我想要得到每個數字從0到9999。每個具體數字是。爲什麼?那麼,我需要它的案件號碼。這是我迄今爲止;

  for (int i = 0; i < 9999; ++i) 
      { 
       if (line.Contains("if(intid == " + i + ")") || line.Contains("if intid == " + i) && !line.EndsWith(" then")) 
       { 
        ErrorBox.Items.Add("ERROR: You're missing then near " + line + " TIME: " + DateTime.Now); 
       } 
      } 

我有什麼上面只會得到最後一個號碼是9999,但我想每一個具體的數字0,1,2,3,4,5,等「我」應該檢索每個號碼最高達到9999.但是,「for」循環無法正常工作。

回答

0

循環看起來很好,我會說這個問題是與你的if語句。試試這個:

if (line.Contains("if(intid == " + i + ")") || line.Contains("if intid == " + i) && !line.EndsWith(" then")) 
{ 
    ErrorBox.Items.Add("ERROR: You're missing then near " + line + " TIME: " + DateTime.Now); 
} 
else 
    ErrorBox.Items.Add("Not an error"); 
0

你已經被我聲明爲int;但是string.contains方法期望看到一個字符串。

那麼,你有我在你的包含,使這個i.ToString()代替:

if (line.Contains("if(intid == " + i.ToString() + ")") || line.Contains("if intid == " + i.ToString()) && !line.EndsWith(" then")) 

我不知道爲什麼它會爲9999而不是其他人的工作。可能是||的邏輯和& &?

Andrew

+0

哇,顯然它把它作爲一個字符串。謝謝,它工作。它現在將獲得每個數字。只有一個問題是它沒有正確地執行line.Contains(「if(intid ==」+ i.ToString()+「))」。 –

0

我想你可能有prescendence的問題。在||之前評估& &。它就像1 + 2 * 3等於7。您需要添加()才能正確評估您的表達。 (B1 || B2)& & B3

編輯

bool b1 = line.Contains("if(intid == " + i + ")"); 
bool b2 = line.Contains("if intid == " + i); 
bool b3 = !line.EndsWith(" then"); 

所以寫

if ((line.Contains("if(intid == " + i + ")") || line.Contains("if intid == " + i)) 
    && !line.EndsWith(" then"))