使用C#在字符数组中分配转义序列
Assignment of escape sequence in character array using C#
我需要修改字符数组,包括换行符 ('\n'),这样当我打印字符数组时,它应该按格式打印元素。
我不知道当我删除循环内的那些评论时它是如何工作的,为什么它在 foreach 循环中不起作用。
有人可以帮我吗???
int row, col;
row = col = 2;
int j;
char[] ch = new char[ row * col + row ];
for( int i = 0; i < 2; i++ ) {
for( j = 0; j < 2; j++ ) {
ch[ i * col + j ] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[ i * col + j ] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach( char c in ch ) {
Console.Write(c);
}
我的输出应该是:
aa
aa
因为您覆盖了您的“\n”,所以您需要为\n 字符保留一列。
这对我有用:
int row, col;
row = col = 2;
col = 2;
int j;
char[] ch = new char[ row * (col+1) ];
for( int i = 0; i < 2; i++ ) {
for( j = 0; j < 2; j++ ) {
ch[ i * (col+1) + j ] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[ i * (col+1) + j ] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach( char c in ch ) {
Console.Write(c);
}
您可以通过输出写入的数组索引轻松检查这些问题,在您的代码中,您将两次写入数组的相同索引。
编辑: 无需分配更多字符 row*col+row
是正确的,但您需要在循环内使用 (col+1)。
编辑 2 更好地使用 (col+1) 也分配数组,现在它只工作因为 row = col.
你正在超越你的价值观。使用下面的代码
int row, col;
row = col = 2;
int j;
char[] ch = new char[row * col + row];
for (int i = 0; i < ch.Length; i++)
{
for (j = 0; j < 2; j++)
{
ch[i + j] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[i + j] = '\n';
i = i + j;
//ch[i * col + (j+1)] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach (char c in ch)
{
Console.Write(c);
}
您的代码正在将值重新分配给同一索引
试试下面的代码:
int row, col;
row = col = 2;
int j;
char[] ch = new char[ row * col + row ];
for( int i = 0; i < 2; i++ ) {
for( j = 0; j < 2; j++ ) {
ch[ i * col + (j+i) ] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[ i * col + (j+i) ] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach( char c in ch ) {
Console.Write(c);
}
就这样:
int col = 3;
您需要多留一列来存储 \n
的值。
我需要修改字符数组,包括换行符 ('\n'),这样当我打印字符数组时,它应该按格式打印元素。
我不知道当我删除循环内的那些评论时它是如何工作的,为什么它在 foreach 循环中不起作用。
有人可以帮我吗???
int row, col;
row = col = 2;
int j;
char[] ch = new char[ row * col + row ];
for( int i = 0; i < 2; i++ ) {
for( j = 0; j < 2; j++ ) {
ch[ i * col + j ] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[ i * col + j ] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach( char c in ch ) {
Console.Write(c);
}
我的输出应该是:
aa
aa
因为您覆盖了您的“\n”,所以您需要为\n 字符保留一列。 这对我有用:
int row, col;
row = col = 2;
col = 2;
int j;
char[] ch = new char[ row * (col+1) ];
for( int i = 0; i < 2; i++ ) {
for( j = 0; j < 2; j++ ) {
ch[ i * (col+1) + j ] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[ i * (col+1) + j ] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach( char c in ch ) {
Console.Write(c);
}
您可以通过输出写入的数组索引轻松检查这些问题,在您的代码中,您将两次写入数组的相同索引。
编辑: 无需分配更多字符 row*col+row
是正确的,但您需要在循环内使用 (col+1)。
编辑 2 更好地使用 (col+1) 也分配数组,现在它只工作因为 row = col.
你正在超越你的价值观。使用下面的代码
int row, col;
row = col = 2;
int j;
char[] ch = new char[row * col + row];
for (int i = 0; i < ch.Length; i++)
{
for (j = 0; j < 2; j++)
{
ch[i + j] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[i + j] = '\n';
i = i + j;
//ch[i * col + (j+1)] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach (char c in ch)
{
Console.Write(c);
}
您的代码正在将值重新分配给同一索引
试试下面的代码:
int row, col;
row = col = 2;
int j;
char[] ch = new char[ row * col + row ];
for( int i = 0; i < 2; i++ ) {
for( j = 0; j < 2; j++ ) {
ch[ i * col + (j+i) ] = 'a';
//Console.Write(ch[ i * col + j ]);
}
ch[ i * col + (j+i) ] = '\n';
//Console.Write(ch[ i * col + j ]);
}
Console.WriteLine("Character Array:");
foreach( char c in ch ) {
Console.Write(c);
}
就这样:
int col = 3;
您需要多留一列来存储 \n
的值。