代码转到无限循环

时间:2013-01-01 20:25:24

标签: c# list infinite-loop

我试图创建一个基本上创建一些二进制数据/转换它的函数,并根据存储它的方程修改它并将输出导出为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);
        }
    }

每当我尝试运行此代码时,我都处于无限循环中,我的代码永远不会结束。我哪里出错了?谢谢!

1 个答案:

答案 0 :(得分:5)

你编写了一个无限循环:

for (; ; )

为什么你会惊讶地无限奔跑?

要么将其删除,要么在需要时将其设为非无限或break