C# 在表C中显示数组2D中的值#
我是C语言编程新手,我正在尝试创建一种方法,允许我创建一个表,并将矩阵中的数据可视化。然而,我尝试过的方法中似乎没有一种像想象的那样有效 基本上,该方法用于可视化头(即枚举),但是我需要从矩阵中读取数据,并且输出必须是表的形式。以下是迄今为止的代码:C# 在表C中显示数组2D中的值#,c#,arrays,C#,Arrays,我是C语言编程新手,我正在尝试创建一种方法,允许我创建一个表,并将矩阵中的数据可视化。然而,我尝试过的方法中似乎没有一种像想象的那样有效 基本上,该方法用于可视化头(即枚举),但是我需要从矩阵中读取数据,并且输出必须是表的形式。以下是迄今为止的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespa
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);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台AP3
{
enum clientHeader{Id、姓名、姓氏、地址、邮政编码、电话、电子邮件、州};
班级计划
{
静态void Main(字符串[]参数)
{
字符串[,]客户端=新字符串[3,7];
插入数据(客户端);
Console.Clear();
showHeader(客户端);
listData(客户端);
Console.ReadKey();
}
静态void insertData(字符串[,]矩阵)
{
int x=矩阵GetLowerBound(1);
矩阵[0,x]=“1”;
对于(int j=1;jEnum.GetName(typeof(T),i);
静态void showHeader(字符串[,]矩阵)
{
int x=矩阵。GetUpperBound(1);
字符串[]数组=新字符串[x];
for(int i=0;i宽度?文本。子字符串(0,宽度-3)+“…”:文本;
if(string.IsNullOrEmpty(text))
{
返回新字符串(“”,宽度);
}
其他的
{
返回text.PadRight(宽度-(宽度-text.Length)/2.PadRight(宽度);
}
}
}
}
在下面的代码中,我所做的主要修改是使您的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();
}
私有静态无效列表数据(字符串[,]矩阵)
{
var数组=新字符串[matrix.GetUpperBound(1)];
for(var i=0;i
看看这对你是否有效。以下是一些其他重构的完整代码:
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;
}
私有静态void InsertData(字符串[,]矩阵)
{
//测试时只需添加虚拟数据
矩阵[0,0]=“1”;
矩阵[0,1]=“FirstName”;
矩阵[0,2]=“LastName”;
矩阵[0,3]=“123主街”;
矩阵[0,4]=“98765”;
矩阵[0,5]=“(123)456-7890”;
矩阵[0,6]=”user@provider.com";
返回;
矩阵[0,矩阵.GetLowerBound(1)]=“1”;
对于(var j=1;jEnum.GetName(typeof(T),i);
私有静态void ShowHeader(字符串[,]矩阵)
{
var数组=新字符串[matrix.GetUpperBound(1)];
for(var i=0;i当前+对齐中心(列,宽度)+“|”);
控制台写入线(世界其他地区);
}
静态字符串对齐中心(字符串文本,整数宽度)
{
文本=文本。长度>宽度?文本。子字符串(0,宽度-3)+“…”:文本;
返回字符串.IsNullOrEmpty(文本)
?新字符串('',宽度)
:text.PadRight(宽度-(宽度-text.Length)/2.PadRight(宽度);
}
enum ClientHeader{Id、姓名、姓氏、地址、邮政编码、电话、电子邮件、州};
私有静态void Main()
{
var client=新字符串[3,7];
插入数据(客户端);
Console.Clear();
ShowHeader(客户端);
ListData(客户端);
GetKeyFromUser(“\n完成!按任意键退出…”);
}
专用静态控制台KeyInfo GetKeyFromUser(字符串提示)
{
控制台。写入(提示);
var key=Console.ReadKey();
Console.WriteLine();
返回键;
}
输出
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();
}
}