繁体   English   中英

三维数组检索值

[英]3 dimensional array retrieve values

我正在编写一个程序,允许用户输入每月每天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);

             }

         }

    }
}

我无法弄清楚如何从三维数组中检索数据,如上所述打印表格

你需要两次遍历你的数组。 您需要一个循环来显示销售人员标题。 您需要嵌套循环来显示您的行。 您可以在第一个内部循环中为行标识符生成文本。 您也可以生成在那里结束的行。 最内层循环可用于显示当天和销售人员的总计数。

虽然这不能直接回答您的问题,但如果您自己更容易回答,可能会有所回答。

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

根据我的理解,将有2个类,一个SalesPerson和一个Product 每个都将“容纳”下一级对象数组,然后您只需在SalesPerson[3]的主方法中使用单维数组SalesPerson[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();
    }
}

希望能帮助到你。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM