为什么 .txt 文件输出中的每一行都以 'System.int32' 结尾? C#
Why does every line in the .txt-file output end with 'System.int32'? C#
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);
}
}
}
}
}
作为 pastebin:
输出为:
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.
我检查了一些其他 post 关于 System.Int32[][]
的外观,但在所有情况下,它是代码没有生成任何输出,除了 System.Int32[]
,这意味着代码可以不解释 for-loop
创建输出。这里显示了正确的输出,但仍然以 System.Int32[][]
结尾。为什么会这样,我做错了什么?
您在内循环后输出了整个数组。如果你想要一个换行符,你可以使用 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("");
}
}
默认情况下,ToString 方法returns对象的类型。因此,如果您在不覆盖 ToString 方法(在 Object class 中定义)的情况下打印非原始类型,您将获得类型名称。
您遇到的问题是:
SW.WriteLine(M);
正如 Heinzbeinz 已经说过的,您正在打印一个数组,该数组在 C# 中将导致打印对象类型,例如:
int[] ar1 = {0,1,2,3};
int[] ar2 = {0,1,2,3};
int[][] m = {ar1,ar2};
Console.WriteLine(m);
将导致打印 System.Int32[][]
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);
}
}
}
}
}
作为 pastebin:
输出为:
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.
我检查了一些其他 post 关于 System.Int32[][]
的外观,但在所有情况下,它是代码没有生成任何输出,除了 System.Int32[]
,这意味着代码可以不解释 for-loop
创建输出。这里显示了正确的输出,但仍然以 System.Int32[][]
结尾。为什么会这样,我做错了什么?
您在内循环后输出了整个数组。如果你想要一个换行符,你可以使用 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("");
}
}
默认情况下,ToString 方法returns对象的类型。因此,如果您在不覆盖 ToString 方法(在 Object class 中定义)的情况下打印非原始类型,您将获得类型名称。
您遇到的问题是:
SW.WriteLine(M);
正如 Heinzbeinz 已经说过的,您正在打印一个数组,该数组在 C# 中将导致打印对象类型,例如:
int[] ar1 = {0,1,2,3};
int[] ar2 = {0,1,2,3};
int[][] m = {ar1,ar2};
Console.WriteLine(m);
将导致打印 System.Int32[][]