繁体   English   中英

Streamwriter仅将循环的第一行打印到CSV文件

[英]Streamwriter is only printing the first line of loop to CSV file

我做了一个快速程序,所以我可以从collat​​z猜想中获取数据以作图,并且我试图将其写入到csv中,并在第1列中显示结果量,并在第2列中显示步数。流写入器仅输出每行的第一行。 如何在写入CSV文件之前更新Streamwriter?

    static void Main(string[] args)

    {

        int y;


        BigInteger x;

        System.Collections.ArrayList ResultsArray = new System.Collections.ArrayList();

        System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\public\testfolder\writelines2.csv");


        x = BigInteger.Pow(15, 15);
        y = 1;

        var ColumnWriter = string.Format("{0},{1}",x, y);
        do
        {

            if (x % 2 != 0)

            {
                x = (x * 3) + 1;
                Console.WriteLine(x);
                ResultsArray.Add(x);
            }
            else
            {

                x = (x / 2);
                Console.WriteLine(x);
                ResultsArray.Add(x);
            }

           file.WriteLine(ColumnWriter);
            y = y + 1;


        } while (x > 1);

        file.Flush();
        Console.ReadLine();
}
}

}

您无需在循环内更新字符串变量ColumnWriter。

ColumnWriter = string.Format("{0},{1}",x, y);

行之前或之后(取决于您要输出的内容/时间)

file.WriteLine(ColumnWriter);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM