以有条理的方式将数据添加到2d数组

时间:2014-11-05 13:02:34

标签: c# arrays loops 2d

我需要创建一个2 d数组。在该数组中有字符串,ID,电阻值和日期,

每次输入这些内容,对于我需要的所有数据,数组大小为7x16,

如何在代码中没有大的长条目的情况下提示我的用户输入代码?我正在阅读我的c#书籍,但我在循环中表现不佳,有人可以指点我的循环,可以帮助我的技能,

任何帮助都非常欢迎,史蒂文

using System;
using System.Globalization;   // Has to be included, not in most docs!
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;

public class CellDetails
{

    public static void Main()
    {
        Console.WriteLine("Enter value for Cell ID, Sample, Write/TX, Depass, Discharge Date and Discharge load");
        String[,] CellDetailArray = new String[7, 16];

        // Sets the element at index 0,0.
        CellDetailArray.SetValue("pmx150_01", 0, 0);
        Console.WriteLine("Cell Identifier   {0}", CellDetailArray.GetValue(0, 0));


        Console.ReadLine();
    }

}

2 个答案:

答案 0 :(得分:0)

我不确定这是否是你想要的,但它可能有所帮助:

static void Main(string[] args)
{
    Console.WriteLine("Enter values for Cell ID, Sample, Write/TX, Depass, Discharge Date and Discharge load.");
    String[,] CellDetailArray = new String[16, 6];

    for (int i = 0; i < CellDetailArray.GetLength(0); i++)
    {
        Console.WriteLine(string.Format("Data for row no. {0}:", i + 1));
        for (int j = 0; j < CellDetailArray.GetLength(1); j++)
        {
            Console.Write(string.Format("\tValue for column no. {0}: ", j + 1));
            CellDetailArray[i, j] = Console.ReadLine();
        }
    }

    Console.WriteLine("Data entry finished.\n\nArray contents:");

    for (int i = 0; i < CellDetailArray.GetLength(0); i++)
    {
        Console.Write(string.Format("\nRow no. {0}: ", i + 1));
        for (int j = 0; j < CellDetailArray.GetLength(1); j++)
        {
            Console.Write(string.Format("\"{0}\"", CellDetailArray[i, j]));
            if (j < CellDetailArray.GetLength(1) - 1)
                Console.Write(", ");
        }
    }
}

注意:你说你的数组必须是7x16,但是你只列出了6个字段,所以我把数组设为6x16(16行,6个字段)。

答案 1 :(得分:0)

这是我的代码,谢谢Andrew,

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

namespace Input_Cell_details_to_Array

{
    class Program
    {
        static void Main(string[] args)
        {
            //string[] CellDataHeaders = new string[7] { "CellID", "Sample", "WriteTX", "Depass", "DischargeDate", "Discharge" };
            Console.WriteLine("Enter values for Cell ID (CellType_Serial_LoadType_Term), Sample, Write/TX, Depass, Discharge Date (dd/mm/yyyy) and Discharge load.");
            String[,] CellDetailArray = new String[16, 7];

            for (int i = 0; i < CellDetailArray.GetLength(0); i++)
            {
                string[] CellDataHeaders = new string[7] { "Cell ID", "Quiescent", "Sample", "WriteTX", "Depass", "DischargeDate", "Discharge" };
                Console.WriteLine("####################################################################################################");
                Console.WriteLine("");
                Console.WriteLine(string.Format("Data for row no. {0}:", i + 1));

                for (int j = 0; j < CellDetailArray.GetLength(1); j++)
                {
                    Console.Write(CellDataHeaders[j]);
                    Console.Write(string.Format("\tValue for column no. {0}: ", j + 1));
                    CellDetailArray[i, j] = Console.ReadLine();
                }
            }
            Console.WriteLine("");
            Console.WriteLine("========================================================================================================");
            Console.WriteLine("Data entry finished.\n\n Cell Detail Array contents:");

            for (int i = 0; i < CellDetailArray.GetLength(0); i++)
            {
                Console.Write(string.Format("\nRow no. {0}: ", i + 1));
                for (int j = 0; j < CellDetailArray.GetLength(1); j++)
                {
                    Console.Write(string.Format("\"{0}\"", CellDetailArray[i, j]));
                    if (j < CellDetailArray.GetLength(1) - 1)
                        Console.Write(", ");
                }
                Console.ReadLine();
            }
        }
    }
}