好吧我有一个
string[,] grid = new string[3, 3] {{" "," "," "},
{" "," "," "},
{" "," "," "}};
我有一个代码,我让用户以这种形式输入x和y的坐标(x,y(例如2,2)),我似乎有的问题是如何实际绘制实际数组中的符号/字符,我将如何在Main?
中进行处理答案 0 :(得分:3)
为了示例,我们假设您要在网格的右下角输入符号$。你这样做:
grid[2,2] = "$";
现在,如果你打印它,你的数组将会是这样的:
{{" "," "," "},
{" "," "," "},
{" "," ","$"}};
不要忘记数组是基于0的,所以在你的情况下没有3个插槽,只有0,1和2。
如果您想知道如何将该网格输出到控制台,请尝试:
int rowLength = grid.GetLength(0);
int colLength = grid.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Console.Write(string.Format("{0} ", grid[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
编辑:您询问了如何获取用户的输入,然后将符号放在指定位置:
Console.WriteLine("Please enter the x-Coordinate");
int x = Console.ReadLine();
Console.WriteLine("Please enter the y-Coordinate");
int y = Console.ReadLine();
grid[x,y] = "$";
//then redraw the grid if you desire using the code above
答案 1 :(得分:0)
这是你需要的吗?
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++) Console.Write(grid[i, j]);
Console.WriteLine()
}