2016-12-14 45 views
-2

好的,我有一個C#如何繪製值到2D陣列與用戶輸入

 string[,] grid = new string[3, 3] {{" "," "," "}, 
              {" "," "," "}, 
              {" "," "," "}}; 

和我具有其中我得到的用戶輸入用於x和y座標在這種類型的代碼form(x,y(例如2,2)),我似乎遇到的問題是如何在實際數組中實際繪製符號/字符,我將如何在Main中繪製符號/字符?

+0

'如何在實際數組中繪製符號/字符???? –

+0

就像我希望編譯器通過提供座標2,2並將編譯器替換爲一個X而不是空白空間來將X繪製到其中一個空格 – Chaus

+2

@Chaus您是否需要幫助瞭解如何編寫符號到那個協調或者幫助理解如何顯示你的網格? –

回答

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 
+0

那麼我將如何定製這個代碼來處理輸出值的子程序XCoordinate和YCoordinate,然後使用這兩個實際輸出該符號到指定的位置? 所以我有一個子程序,提示用戶輸入值,例如2,2,我將如何得到這些值在該代碼中使用? – Chaus

+0

好的,然後你想把用戶的輸入存儲到一個變量中,我會在我的答案中顯示你,我將添加一個編輯 –

+0

盛大謝謝:) – Chaus

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() 
}