2016-04-30 34 views
-1
string userName = Console.ReadLine(); 


string[,] stats = new string[5,5]; 
stats[1,0] = "Name: "; 
stats[1,1] = "userName"; 

我知道这可能不是做事的最有效的方式,但林新,只是用C#玩弄。我将如何显示这个?每次我尝试它时,总是将它们显示在彼此之上。如何在网格中显示2d数组?

+0

你是什么意思的“显示”?你的意思是将它们打印到控制台上?你试过了什么代码? – erisco

+0

请更具体。 – ViVi

回答

2

随着一个循环:

for (var i = 0; i < stats.GetLength(0); i++) { 
    for (var j = 0; j < stats.GetLength(1); j++) 
    Console.Write("{0} ", stats[i, j]); 
    Console.WriteLine(); 
} 

这是一个基于LINQ的解决方案:

string[,] stats = new string[3,3] { 
    { "Name:", "userName", "some stat" }, 
    { "More stat:", "more", "more" }, 
    { "Even more:", "hey", "great" } 
}; 
var lines = stats.Cast<string>() 
       .Select((v, i) => new { Idx = i, Val = v }) 
       .GroupBy(x => x.Idx/stats.GetLength(1)) 
       .Select(x => string.Join(" ", x.Select(y => y.Val))); 
Console.WriteLine(string.Join(Environment.NewLine, lines)); 

打印

名称:用户名的一些统计

更多的数据:更多更多

甚至更​​多:嘿伟大