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);
        }
    }

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

4

1 回答 1

5

您编写了一个无限循环:

for (; ; )

你为什么对它无限运行感到惊讶?

要么删除它,要么让它成为非无限的,要么break在需要时将其移除。

于 2013-01-01T20:26:37.220 回答