簡體   English   中英

讀取數組2D並輸出為表C#

[英]Read array 2D and output as table C#

我是C#編程的新手,我試圖以表格的形式顯示矩陣的全部內容,但是到目前為止,我得到的是讀取枚舉並從矩陣中讀取一行。 相反,我需要從矩陣中讀取多行並將其輸出為表格。

當我運行程序時,我會在行中插入兩次數據,並且輸出應該是表中的該數據,但是只顯示了一行。 這是代碼:

static int getInsertIndex(string[,] matrix)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (string.IsNullOrEmpty(matrix[j, 0])) return j;
            }

            return -1;
        }
        private static void InsertData<T>(string[,] matrix)
        {

            // int newId = generateId(ref id);
            int n = getInsertIndex(matrix), id = 1;

            matrix[n, 0] = Convert.ToString(id++);
            int x = matrix.GetLength(1) - 1;
            matrix[n, x] = "true";

            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 l = 0; l < matrix.GetLength(0); l++)
            {
                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[4, 7];
            InsertData<ClientHeader>(client);

            Console.Clear();
            InsertData<ClientHeader>(client);

            ShowHeader<ClientHeader>(client);
            ListData<ClientHeader>(client);

        }
    }
}

有兩個問題:

1)在InsertData()函數中,您要更新第n行。

更換

matrix[0, j] = Console.ReadLine();

通過

matrix[n, j] = Console.ReadLine();

2)在ListData()函數中要顯示每一行,因此需要將數組變量移到第一個for循環中。 array[i] = matrix[l, i]替換array[i] = matrix[0, i] ,因為您正在顯示第l行。

private static void ListData<T>(string[,] matrix)
{
    for (var l = 0; l < matrix.GetLength(0); l++)
    {
        var array = new string[matrix.GetUpperBound(1)];

        for (var i = 0; i < array.Length; i++)
        {
            array[i] = matrix[l, i];
        }

        PrintRow(array);
    }

    PrintLine();
}

嘗試以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication1
{
    public class Program
    {
        private static void Main()
        {
            List<List<string>> data = new List<List<string>>() {
                new List<string>() { "Name", "Age", "Weight"},
                new List<string>() { "John", "33", "180"},
                new List<string>() { "Mary", "32", "125"},
                new List<string>() { "Harry", "40", "200"}
            };

            DataTable dt = new DataTable();
            for (int i = 0; i < data.Count; i++)
            {
                if (i == 0)
                {
                    foreach (string col in data[i])
                    {
                        dt.Columns.Add(col);
                    }
                }
                else
                {
                    dt.Rows.Add(data[i].ToArray());
                }
            }

        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM