好的,我有一個C#如何繪製值到2D陣列與用戶輸入
string[,] grid = new string[3, 3] {{" "," "," "},
{" "," "," "},
{" "," "," "}};
和我具有其中我得到的用戶輸入用於x和y座標在這種類型的代碼form(x,y(例如2,2)),我似乎遇到的問題是如何在實際數組中實際繪製符號/字符,我將如何在Main中繪製符號/字符?
好的,我有一個C#如何繪製值到2D陣列與用戶輸入
string[,] grid = new string[3, 3] {{" "," "," "},
{" "," "," "},
{" "," "," "}};
和我具有其中我得到的用戶輸入用於x和y座標在這種類型的代碼form(x,y(例如2,2)),我似乎遇到的問題是如何在實際數組中實際繪製符號/字符,我將如何在Main中繪製符號/字符?
對於例子的緣故,假設您想在網格的右下角輸入符號$。你這樣做是這樣的:
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
這是您需要的嗎?
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()
}
'如何在實際數組中繪製符號/字符???? –
就像我希望編譯器通過提供座標2,2並將編譯器替換爲一個X而不是空白空間來將X繪製到其中一個空格 – Chaus
@Chaus您是否需要幫助瞭解如何編寫符號到那個協調或者幫助理解如何顯示你的網格? –