C# 三维数组检索值

C# 三维数组检索值,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我正在编写一个程序,允许用户输入每月每天3名销售人员和5种产品的销售金额。我使用一个三维数组来存储数据。我想以表格格式打印我的数据,其中列为3名销售人员,行为5种产品,每个金额为当月产品的总销售额,即31个值的总和。我还需要在每一列和每一行的末尾有交叉总计 这是我的代码: class Program { static void Main(string[] args) { Slip [,,] sales = new Slip[3, 5, 31];

我正在编写一个程序,允许用户输入每月每天3名销售人员和5种产品的销售金额。我使用一个三维数组来存储数据。我想以表格格式打印我的数据,其中列为3名销售人员,行为5种产品,每个金额为当月产品的总销售额,即31个值的总和。我还需要在每一列和每一行的末尾有交叉总计

这是我的代码:

class Program
{
    static void Main(string[] args)
    {

        Slip [,,] sales = new Slip[3, 5, 31];
        for (int day = 1; day <= 31; day++)
        {
            for (int i = 1; i <= 3; i++)
            {
                Console.WriteLine("Enter the salesperson number of #" + i + ":");
                int salesPersonNumber = Convert.ToInt16(Console.ReadLine());
                Console.WriteLine("Enter the information for day " + day + ", salesperson " + salesPersonNumber + " below:");
                for (int j = 1; j <=5; j++)
                {

                    Console.WriteLine("Enter the product number for product " + j + ":"); 
                    int productNumber = Convert.ToInt16(Console.ReadLine());
                    Console.WriteLine("Enter the total dollar value of the product sold day " + day + ":");
                    decimal value = Convert.ToDecimal(Console.ReadLine());
                    Slip slip = new Slip(salesPersonNumber, productNumber, value);
                    sales[i-1, j-1, day-1] = slip;

                }
            }
        }



        for (int i = 0; i < sales.GetLength(0); i++){ 
            for (int j = 0; j < sales.GetLength(1); j++){
                decimal total = 0;
                for (int k = 0; k < sales.GetLength(2); k++){
                    total += sales[i, j, k].ValueSold;
                }

                Console.WriteLine(total);

             }

         }

    }
}
类程序
{
静态void Main(字符串[]参数)
{
单据[,]销售额=新单据[3,5,31];

对于(int day=1;day您需要在数组中循环两次。您需要一个循环来显示sales people标题。您需要嵌套的循环来显示行。您可以在第一个内部循环中为行标识符day生成文本。您还可以生成以该行结尾的行。最内部的循环可用于显示当天和销售人员的总数。

虽然这并不能直接回答您的问题,但这可能会让您更容易回答

您是否考虑过使用对象而不是多维数组?这将使跟踪所有内容变得更加容易,对于像这样的复杂结构,这是一种更好的做法。它还允许您抽象计算;例如获取一个月内销售的产品总数

据我所知,将有两个类,一个
销售人员
和一个
产品
。每个类将“容纳”下一级对象数组,然后您只需在主要方法中为
销售人员[3]
设置一个一维数组即可。类似这样:

/// <summary>
/// A sales person
/// </summary>
public class SalesPerson
{
    /// <summary>
    /// Gets or sets an array of products
    /// </summary>
    public Product[] Products { get; set; }

    /// <summary>
    /// Constructs a new sales person class, constructing a new products array
    /// </summary>
    public SalesPerson()
    {
        this.Products = new Product[5];
    }
}

/// <summary>
/// A product
/// </summary>
public class Product
{
    /// <summary>
    /// Gets or sets the sales amount array
    /// </summary>
    public int[] SalesAmount { get; set; }

    /// <summary>
    /// Constructs a new product, constructing a new sales amount array
    /// </summary>
    public Product()
    {
        this.SalesAmount = new int[31];
    }

    /// <summary>
    /// Gets the total sold
    /// </summary>
    public int GetTotalSold()
    {
        return this.SalesAmount.Sum();
    }
}
//
///销售人员
/// 
公开课销售员
{
/// 
///获取或设置产品数组
/// 
公共产品[]产品{get;set;}
/// 
///构造一个新的sales person类,构造一个新的products数组
/// 
公共销售人员()
{
本.产品=新产品[5];
}
}
/// 
///产品
/// 
公共类产品
{
/// 
///获取或设置销售金额数组
/// 
public int[]销售金额{get;set;}
/// 
///构造新产品,构造新销售额数组
/// 
公共产品()
{
this.SalesAmount=新整数[31];
}
/// 
///获取总销售额
/// 
public int GetTotalSold()
{
返回此.SalesAmount.Sum();
}
}

希望有帮助。:

问题:您是否每次都键入3X5X31值?是的,每次显示您知道如何获取元素、如何打印行以及如何迭代项目时,都会输入销售人员、产品和值编号……那么您到底有什么问题?(顺便说一句,请尝试使用
for(int i=0;i