2013-01-01 104 views
0

我試圖創建一個函數,基本上創建一些二進制數據/轉換它,並根據方程修改它存儲它並以HTML格式導出輸出。這裏是截斷的代碼:代碼無限循環

 /* 
     * Initialising list/list of list to store data strings. 
     */ 

     List<string> BitValues = new List<string>(); 
     List<List<string>> DataList = new List<List<string>>(); 

     /* 
     * Some Code. 
     */ 


      /* 
      * +=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 
      * Mathematical Code Begins. 
      * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 
      */ 

       List<string> strVal = new List<string>(); 
       double avgVal = 0; 
       for (; ;) 
       { 
        foreach (string randBit in BitValues) 
        { 
         double decVal = Convert.ToDouble(Conversions.ToDecimal(randBit)); 
         /* 
         * Implementing mathematical equation. 
         */ 
         double eqnValue = (0.52359) + (((1.04719)/Math.Pow(2.0, Convert.ToDouble(txtBitSize.Text)) * decVal)); 
         avgVal += Math.Sin(eqnValue); 
         strVal.Add(Convert.ToString(Math.Sin(eqnValue))); 
        } 

        /* 
        * Calculating average value. Adding list to list. 
        */ 

        avgVal /= Convert.ToDouble(txtPopSize.Text); 
        DataList.Add(strVal); 
        List<string> NewStrVal = new List<string>(); 
        if (GlobalVar.Extrema == 2) 
        { 
         for (int i = 0; i < BitValues.Count; i++) 
         { 
          if (avgVal <= Convert.ToDouble(strVal[i])) 
          { 
           NewStrVal.Add(BitValues[i]); 

          } 

          else 
          { 
           BitValues[i] = Conversions.ToComplement(Convert.ToInt32(BitValues[i])); 
           NewStrVal.Add(BitValues[i]); 
          }        
         } 

         DataList.Add(NewStrVal); 
        } 

        /* 
        * +=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 
        * File Writing Code Begins 
        * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 
        */ 

TextWriter tw = new StreamWriter("OUT.html"); 
foreach (IList<string> name in DataList) 
        { 
         tw.WriteLine(tableOuterStart); 
         tw.WriteLine(tableInnerOpen); 
         foreach (string listVal in name) 
         { 
          tw.WriteLine(DataInnerOpen); 
          tw.WriteLine(listVal); 
          tw.WriteLine(DataInnerClose); 
         } 
         tw.WriteLine(tableInnerClose); 
         tw.WriteLine(tableOuterClose); 
        } 

        tw.WriteLine(pageEnd); 


        /* 
        * Close stream 
        */ 

        tw.Close(); 

這裏是適當方法的類。

public class Conversions 
    {  
     public static string ToDecimal(string BitValue) 
     { 
      string ConvertedToDecimal = Convert.ToInt32(BitValue, 2).ToString(); 
      return ConvertedToDecimal; 
     } 

     public static string ToBinary(int DecimalValue) 
     { 
      string ConvertedToBinary = Convert.ToString(DecimalValue, 2); 
      return ConvertedToBinary; 
     } 

     public static string ToComplement(int n) 
     { 
      char[] b = new char[32]; 
      int pos = 31; 
      int i = 0; 

      while (i < 32) 
      { 
       if ((n & (1 << i)) != 0) 
        b[pos] = '1'; 
       else 
        b[pos] = '0'; 
       pos--; 
       i++; 
      } 
      return new string(b); 
     } 
    } 

每當我嘗試運行此代碼,我摔倒在一個無限循環和我的代碼永遠不會結束。我哪裏錯了?謝謝 !

回答

5

您編碼一個無限循環:

for (; ;) 

你爲什麼驚訝它運行無限?

要麼刪除它,使其成爲非無限的,或者在需要時將它移出它break

+0

謝謝。我認爲我的合作伙伴編碼了它,而我在運行數據集時忽略了它。謝謝。 –

+0

是的,for(;;)'發音爲'永遠'。 –

+0

@亨克 - 赫赫......我不會忘記'(;;)'發音爲'ever'。謝謝你:) – Oded