C# 以有条不紊的方式向二维阵列添加数据

C# 以有条不紊的方式向二维阵列添加数据,c#,arrays,loops,2d,C#,Arrays,Loops,2d,我需要创建一个二维阵列。在该数组中有字符串,带有ID、电阻值和日期 每次都要输入这些,我需要的所有数据的数组大小都是7x16 如果代码中并没有大而长的条目,如何提示用户输入代码?我正在读我的c语言书,但我在循环方面很弱,有人能给我指出一个for-of循环来帮助我提高这方面的技能吗 史蒂文,欢迎任何帮助 using System; using System.Globalization; // Has to be included, not in most docs! using System.

我需要创建一个二维阵列。在该数组中有字符串,带有ID、电阻值和日期

每次都要输入这些,我需要的所有数据的数组大小都是7x16

如果代码中并没有大而长的条目,如何提示用户输入代码?我正在读我的c语言书,但我在循环方面很弱,有人能给我指出一个for-of循环来帮助我提高这方面的技能吗

史蒂文,欢迎任何帮助

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();
    }

}

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

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个字段。

这是我的代码,谢谢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();
            }
        }
    }
}

优秀的Andrew,尽可能多地阅读msdn链接和RTFM'ing,我将运行此代码并了解其中发生的情况,我可能有更多字段要输入,我太困惑了,忘记了什么是列,什么是行!love loops in loops,,Steven使用了你给我的代码,我现在对它的工作原理有了更多的了解,非常感谢,我已经对它进行了修改,添加了必要的标题,以便于输入和输出,非常感谢Andrew,太棒了!一些注释:1将CellDataHeader放在您注释它的地方。它是一个对整个程序有意义的数组,因此不应该在循环中声明它。这样声明16个字符串数组。2既然已经有了字段名,就不需要显示列号。字段名更好。3您可以使用ReadKey而不是ReadLine,这样任何键都可以解除程序的暂停,而不仅仅是。4当包含C代码时,使用编辑器中的代码示例{},Ctrl+K选项。您使用的代码片段选项仅适用于HTML、Javascript和CSS。好的,我将研究这些要点并进行更改,,,再次感谢andrew,这是一种荣幸