[英]How to print char[,] in C#?
using System;
namespace cis237_assignment2
{
class Program
{
/// <summary>
/// This is the main entry point for the program.
/// You are free to add anything else you would like to this program,
/// however the maze solving part needs to occur in the MazeSolver class.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
// Starting Coordinates.
const int X_START = 1;
const int Y_START = 1;
// The first maze that needs to be solved.
// Note: You may want to make a smaller version to test and debug with.
// You don't have to, but it might make your life easier.
char[,] maze1 =
{ { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ '#', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
// Create a new instance of a mazeSolver.
MazeSolver mazeSolver = new MazeSolver();
Console.WriteLine("{0}", maze1);
// Create the second maze by transposing the first maze
char[,] maze2 = transposeMaze(maze1);
// Solve the original maze.
mazeSolver.SolveMaze(maze1, X_START, Y_START);
// Solve the transposed maze.
mazeSolver.SolveMaze(maze2, X_START, Y_START);
}
/// <summary>
/// This method will take in a 2 dimensional char array and return
/// a char array maze that is flipped along the diagonal, or in mathematical
/// terms, transposed.
/// ie. if the array looks like 1, 2, 3
/// 4, 5, 6
/// 7, 8, 9
/// The returned result will be:
/// 1, 4, 7
/// 2, 5, 8
/// 3, 6, 9
/// The current return statement is just a placeholder so the program
/// doesn't complain about no return value.
///
/// It is important that you return a "new" char array as the transposed maze.
/// If you do not, you could end up only solving the transposed maze.
/// </summary>
/// <param name="mazeToTranspose"></param>
/// <returns>transposedMaze</returns>
static char[,] transposeMaze(char[,] mazeToTranspose)
{
//Write code her to create a transposed maze.
return new char[1, 1];
}
}
} }
I'll assume you're asking on how to iterate over a Multidimensional array ..我假设您问的是如何迭代多维数组..
The link could provide a great help for you, however, in short, if you know it's 2-D, it's pretty straight-forward该链接可以为您提供很大的帮助,但是,简而言之,如果您知道它是 2-D,那就很简单了
///char[,] arr2D
for (int i=0; i<arr2D.GetLength(0); i++)
{
for(int j=0; j<arr2D.GetLength(1); j++)
Console.Write((j>0)?", ":"" + arr2D[i, j]);
Conole.WriteLine();
}
For fun, here's how to print it in one line of code.为了好玩,这里是如何在一行代码中打印它。
Console.Write(new string(maze1.Cast<char>().SelectMany( (c, i) => (i + 1) % (maze1.GetUpperBound(0) + 1) == 0 ? new[] { c, '\r', '\n' } : new[] { c } ).ToArray()));
Or to spread it out a little:或者稍微展开一下:
Console.Write
(
new string
(
maze1.Cast<char>().SelectMany
(
(c, i) => (i + 1) % (maze1.GetUpperBound(0) + 1) == 0
? new[] { c, '\r', '\n' }
: new[] { c }
)
.ToArray()
)
);
I wouldn't recommend writing it this way in a commercial code base, but it might impress someone.我不建议在商业代码库中以这种方式编写它,但它可能会给某人留下深刻印象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.