为什么 .txt 文件输出中的每一行都以“System.int32"结尾?C# [英] Why does every line in the .txt-file output end with 'System.int32'? C#

查看:38
本文介绍了为什么 .txt 文件输出中的每一行都以“System.int32"结尾?C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#,控制台应用程序,虚拟工作室 2015,并行:Windows 8.1.

C#, Console Application, Virtual Studio 2015, Paralelles: Windows 8.1.

代码如下:

class txt_program
{
    public void txt()
    {
        /* 0 */
        int[] M_array_0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 1 */
        int[] M_array_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 2 */
        int[] M_array_2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        // etc.

        // the M matrix
        int[][] M = { M_array_0, M_array_1, M_array_2, M_array_3, M_array_4,
        M_array_5, M_array_6, M_array_7, M_array_8, M_array_9, M_array_10,
        M_array_12, M_array_13 };

        // the time pick is beeing called into this item.
        Time_pick_2 T2 = new Time_pick_2();

        // the if loop making

        // 0
        if (T2.M_build_0() == true)
        {
            int nr_0 = 0;
            for (int i = nr_0; i < nr_0 + 2; i++)
            {
                M[1][i] = M_array_0[i] + 1;
            }
        }

        // 1
        else if (T2.M_build_1() == true)
        {
            int nr_1 = 1;
            for (int i = nr_1; i < nr_1 + 2; i++)
            {
                M[1][i] = M_array_1[i] + 1;
            }
        }

        // 2
        else if (T2.M_build_2() == true)
        {
            int nr_2 = 2;
            for (int i = nr_2; i < nr_2 + 2; i++)
            {
                M[2][i] = M_array_2[i] + 1;

            }
        }

        // etc.



        using (StreamWriter SW = new StreamWriter(@"txt.txt"))
            {
                // the loops creating the .txt-file.
                for (int i = 0; i < M.GetLength(0); i++)
                {
                    for (int a = 0; a < 13; a++)
                    {
                        SW.Write(" " + M[i][a]);
                    }
                    SW.WriteLine(M);
                }
            }
        }
    }
}

作为粘贴箱:

http://pastebin.com/3HDYwzzZ

输出如下:

0 0 0 0 0 0 0 0 0 0 0 0 0System.Int32[][]
0 1 1 0 0 0 0 0 0 0 0 0 0System.Int32[][]
0 0 0 0 0 0 0 0 0 0 0 0 0System.Int32[][]
// etc.

我查看了一些关于 System.Int32[][] 外观的其他帖子,但在所有情况下,代码除了 System.Int32[],这意味着代码无法解释创建输出的 for-loop.这里显示了正确的输出,但仍然以 System.Int32[][] 结尾.为什么会这样,我做错了什么?

I checked some other post about the appearance off System.Int32[][] but in all of the cases it was code that didnot generate any output except System.Int32[], which meant that the code could not interpret the for-loopcreating the output. Here the correct output is displayed but still with the System.Int32[][] end. Why is this and what am I doing wrong?

推荐答案

您正在内部循环后输出整个数组.如果你想要一个换行符,你可以使用 Console.WriteLine("")

You are outputting the whole array after your inner loop. If you want a newline you could use Console.WriteLine("")

using (StreamWriter SW = new StreamWriter(@"txt.txt"))
{
    // the loops creating the .txt-file.
    for (int i = 0; i < M.GetLength(0); i++)
    {
        for (int a = 0; a < 13; a++)
        {
            SW.Write(" " + M[i][a]);
        }
        //SW.WriteLine(M); // <-- this line was generating your output
        SW.WriteLine("");
    }
}

这篇关于为什么 .txt 文件输出中的每一行都以“System.int32"结尾?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆