c# - 如何在 C# 中打印 char[,]?
问题描述
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];
}
}
}
解决方案
我假设您在询问如何迭代多维数组..
该链接可以为您提供很大的帮助,但是,简而言之,如果您知道它是二维的,那么它非常简单
///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();
}
推荐阅读
- c# - 使用通用列表添加连接服务的 Visual Studio 项目失败
- python - 客户端和服务器之间的连接失败,发送/接收不起作用(python)
- javascript - 每当单击邀请按钮时,每次都应添加新表单,当单击删除按钮的表单被删除时,任何人都可以帮助我吗?
- control-flow - 我对 Powershell 脚本中条件语句的行为感到困惑
- python - 在 Python 中排序 LinkedList
- firebase - Firebase 电话身份验证在已投入生产的应用程序上失败,状态码为 17028
- ruby-on-rails - Rails datetime_field 如何删除特定的工作日?
- java - 使用 Spring WebFlux 构建递归数据结构
- c# - 是否可以将 DataGridCell 的样式移动到另一个单独的文件?
- c++ - 查找数组中的对数,使得数组中存在的这些对之间的所有元素都小于对本身