[英]Display values from array 2D inside table C#
I am new at programming in C# and I'm trying to create a method that allows me to create a table and visualize the data inside of a matrix. 我是C#编程的新手,我正在尝试创建一种方法,该方法允许我创建表并可视化矩阵内的数据。 However none of the methods I have tried seems to work as supposed. 但是,我尝试过的任何方法似乎都无法按预期工作。
Basically the method works to visualize the header which is the Enum, however I need to read the data from the matrix and the output has to be in the form of a table. 基本上,该方法可以使标题(即Enum)可视化,但是我需要从矩阵中读取数据,并且输出必须采用表格形式。 Here´s the code so far: 到目前为止的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
enum clientHeader { Id, Name, Surname, Addres, Cod_Postal, Telephone, Email, State };
class Program
{
static void Main(string[] args)
{
string[,] client = new string[3, 7];
insertData<clientHeader>(client);
Console.Clear();
showHeader<clientHeader>(client);
listData<clientHeader>(client);
Console.ReadKey();
}
static void insertData<T>(string[,] matrix)
{
int x = matrix.GetLowerBound(1);
matrix[0, x] = "1";
for (int j = 1; j < matrix.GetLength(1); j++)
{
do
{
Console.Write($"\nInsert {GetHeader<T>(j)}: ");
matrix[0, j] = Console.ReadLine();
} while (String.IsNullOrEmpty(matrix[0, j]));
}
}
static void listData<T>(string[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write($"{matrix[i, j]}\t");
}
}
Console.WriteLine();
}
}
private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);
static void showHeader<T>(string[,] matrix)
{
int x = matrix.GetUpperBound(1);
string[] array = new string[x];
for (int i = 0; i < array.Length; i++)
{
array[i] = GetHeader<T>(i);
}
PrintLine();
PrintRow(array);
PrintLine();
}
static int tableWidth = 120;
static void PrintLine()
{
Console.WriteLine(new string('-', tableWidth));
}
static void PrintRow(params string[] columns)
{
int width = (tableWidth - columns.Length) / columns.Length;
string row = "|";
foreach (string column in columns)
{
row += AlignCentre(column, width) + "|";
}
Console.WriteLine(row);
}
static string AlignCentre(string text, int width)
{
text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;
if (string.IsNullOrEmpty(text))
{
return new string(' ', width);
}
else
{
return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
}
}
}
}
In the code below, the main modification I made was to have your ListData
method act like your ShowHeader
method, so that the data is aligned in columns underneath the header: 在下面的代码中,我所做的主要修改是使ListData
方法的行为类似于ShowHeader
方法,以便数据在标头下方的列中对齐:
private static void ListData<T>(string[,] matrix)
{
var array = new string[matrix.GetUpperBound(1)];
for (var i = 0; i < array.Length; i++)
{
array[i] = matrix[0, i];
}
PrintRow(array);
PrintLine();
}
See if that works for you. 看看是否适合您。 Here's the full code with some other refactoring: 这是带有其他重构的完整代码:
private static void InsertData<T>(string[,] matrix)
{
// Just add dummy data while testing
matrix[0, 0] = "1";
matrix[0, 1] = "FirstName";
matrix[0, 2] = "LastName";
matrix[0, 3] = "123 Main St";
matrix[0, 4] = "98765";
matrix[0, 5] = "(123) 456-7890";
matrix[0, 6] = "user@provider.com";
return;
matrix[0, matrix.GetLowerBound(1)] = "1";
for (var j = 1; j < matrix.GetLength(1); j++)
{
do
{
Console.Write($"\nInsert {GetHeader<T>(j)}: ");
matrix[0, j] = Console.ReadLine();
} while (string.IsNullOrEmpty(matrix[0, j]));
}
}
private static void ListData<T>(string[,] matrix)
{
var array = new string[matrix.GetUpperBound(1)];
for (var i = 0; i < array.Length; i++)
{
array[i] = matrix[0, i];
}
PrintRow(array);
PrintLine();
}
private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);
private static void ShowHeader<T>(string[,] matrix)
{
var array = new string[matrix.GetUpperBound(1)];
for (var i = 0; i < array.Length; i++)
{
array[i] = GetHeader<T>(i);
}
PrintLine();
PrintRow(array);
PrintLine();
}
private static void PrintLine()
{
Console.WriteLine(new string('-', Console.WindowWidth - 1));
}
private static void PrintRow(IReadOnlyCollection<string> columns)
{
var width = (Console.WindowWidth - 1 - columns.Count) / columns.Count;
var row = columns.Aggregate("|", (current, column) => current + AlignCentre(column, width) + "|");
Console.WriteLine(row);
}
static string AlignCentre(string text, int width)
{
text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;
return string.IsNullOrEmpty(text)
? new string(' ', width)
: text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
}
enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };
private static void Main()
{
var client = new string[3, 7];
InsertData<ClientHeader>(client);
Console.Clear();
ShowHeader<ClientHeader>(client);
ListData<ClientHeader>(client);
GetKeyFromUser("\nDone! Press any key to exit...");
}
private static ConsoleKeyInfo GetKeyFromUser(string prompt)
{
Console.Write(prompt);
var key = Console.ReadKey();
Console.WriteLine();
return key;
}
Output 产量
I tried doing like this, don't know if it's what you meant, however the program crashes. 我试图这样做,不知道这是否是您的意思,但是程序崩溃了。 Still doesn't read second line, I think the variable text from the method AlignCenter is null for some reason. 仍然不读第二行,出于某种原因,我认为AlignCenter方法中的变量文本为null。
private static void ListData<T>(string[,] matrix)
{
var array = new string[matrix.GetUpperBound(1)];
for (int j = 0; j < array.GetUpperBound(0); j++)
{
for (var i = 0; i < array.Length; i++)
{
array[i] = matrix[j, i];
}
PrintRow(array);
PrintLine();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.