c# looping 只打印第一个循环

c# looping only prints first loop

我有一个函数可以读取整个文件,然后有一个单独的函数可以将内容打印到文本文档中,但它只在第一个循环中打印。

我的代码是:

private void LineModule ( StreamReader reader,StreamWriter writer1 = null)      //Interpretes line objects in the DXF file
{
    // creates a StreamWriter ready to write data to a file
    using (StreamWriter writer = new StreamWriter("blahblah.txt"))
    {
        string line1, line2;
        line1 = "0";
        line2 = "0";          
        double x1= 0;
        double y1 = 0;
        double x2= 0;
        double y2 = 0;

        do
        {
            GetLineCouple(reader, out line1, out line2);

            if (line2 != "EOF")
            { }
            if (line1 == "10")
            {
                x1 = Convert.ToDouble(line2);

                if (x1 > XMax)
                    XMax = x1;

                if (x1 < XMin)
                    XMin = x1;
            }

            if (line1 == "20")
            {
                y1 = Convert.ToDouble(line2);

                if (y1 > YMax)
                    YMax = y1;

                if (y1 < YMin)
                    YMin = y1;
                string xyz1 = (x1 + "," + line2 + ",0.0");
                xyz1 = xyz1.TrimEnd();
                writer.WriteLine(xyz1);
            }

            if (line1 == "11")
            {
                x2 = Convert.ToDouble(line2);

                if (x2 > XMax)
                    XMax = x2;

                if (x2 < XMin)
                    XMin = x2;
            }

            if (line1 == "21")
            {
                y2 = Convert.ToDouble(line2);

                if (y2 > YMax)
                    YMax = y2;

                if (y2 < YMin)
                    YMin = y2;
                string xyz2 = (x2 + "," + line2 + ",0.0\n");
                xyz2 = xyz2.TrimEnd();
                writer.WriteLine(xyz2);
            }
        }
        while (line1 != "21");

第一个循环正是我想要的,但我希望它将所有循环打印到文件中。

以及 getlinecouple 函数:

private void GetLineCouple(StreamReader theReader, out string line1, out string line2, StreamWriter writer1 = null)     //this method is used to iterate through the text file and assign values to line1 and line2
{
    System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
    string decimalSeparator = ci.NumberFormat.CurrencyDecimalSeparator;

    {
        line1 = line2 = "";
        if (theReader == null)
            return;

        line1 = theReader.ReadLine();
        if (line1 != null)
        {
            line1 = line1.Trim();
            line1 = line1.Replace('.', decimalSeparator[0]);
            // writer1.WriteLine(line1);
        }
        line2 = theReader.ReadLine();

        if (line2 != null)
        {
            line2 = line2.Trim();
            line2 = line2.Replace('.', decimalSeparator[0]);
            //writer1.WriteLine(line2);
        }
    }
    ...
}

调试 GetLineCouple,并观察 line1 值。我猜它在第一次迭代期间设置为 21 in。当它转到第 != 21 行时,它 returns false 并退出 while 循环。

第一次循环后line1的值是“21”吗?

您应该 运行 调试器并单步执行代码。这样你就可以看到每一步的变量值,并可以检查你的退出条件。

我重构了您的 LineModule 方法以提取一些重复的代码并修复了一些问题:

private void LineModule ( StreamReader reader,StreamWriter writer1 = null)     //Interpretes line objects in the DXF file
{
    // creates a StreamWriter ready to write data to a file
    using (StreamWriter writer = new StreamWriter("blahblah.txt"))
    {

        string line1 = "0", line2 = "0";
        double x1= 0, x2 = 0, y1 = 0, y2 = 0;

        do
        {
            GetLineCouple(reader, out line1, out line2);

            switch (line1)
            {
                case "10":
                    x1 = Convert.ToDouble(line2);
                    SetMaxMin(x1, ref XMax, ref XMin);
                    break;
                case "20":
                    y1 = Convert.ToDouble(line2);
                    SetMaxMin(y1, ref YMax, ref YMin);
                    PrintXYX(x1, line2, writer);
                    break;
                case "11":
                    x2 = Convert.ToDouble(line2);
                    SetMaxMin(x2, ref XMax, ref XMin);
                    break;
                case "21":
                    y2 = Convert.ToDouble(line2);
                    SetMaxMin(y2, ref YMax, ref YMin);
                    PrintXYZ(x2, line2, writer);
                    break;
                default:
                    // deal with other values here
                    break;
            }
        }
        while (line1 != "21");
    }
}

private static void PrintXYZ(double value, string lineValue, StreamWriter writer)
{
    string xyz = (value + "," + lineValue + ",0.0");
    writer.WriteLine(xyz);
}

private static void SetMaxMin(double value, ref double max, ref double min)
{
    if (value > max)
    {
        max = value
    }

    if (value < min)
    {
        min = value;
    }
} 

谢谢大家的回答。

我通过将 while != "21" 更改为 while != "EOF" 然后循环遍历整个文件来解决这个问题。

现在唯一的问题是程序崩溃了,但它确实输出了正确的信息。

再次感谢您