简体   繁体   English

三维数组检索值

[英]3 dimensional array retrieve values

I am writing a program which allows the user to enter in sales amounts for 3 salepeople and 5 products for every day of the month. 我正在编写一个程序,允许用户输入每月每天3个销售人员和5个产品的销售额。 I am using a 3 dimensional array to store the data. 我使用三维数组来存储数据。 I would like to print my data in a tabular format with columns for the 3 salespeople and rows for the five products with each amount being the total sales of the product for the month ie the sum of the 31 values. 我想以表格格式打印我的数据,其中包括3个销售人员的列和5个产品的行,每个数量是该月产品的总销售额,即31个值的总和。 Also I need to have the cross totals at the end of every column and row 此外,我需要在每列和每行的末尾都有交叉总计

this is my code: 这是我的代码:

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

             }

         }

    }
}

I can't figure out how to retrieve the data from the three dimensional array as I described above to print a table 我无法弄清楚如何从三维数组中检索数据,如上所述打印表格

You need to loop through your array twice. 你需要两次遍历你的数组。 You need a single loop to display the sales people header. 您需要一个循环来显示销售人员标题。 You need the nested loop to display your rows. 您需要嵌套循环来显示您的行。 You can generate the text for the row identifier, the day, in the first inner loop. 您可以在第一个内部循环中为行标识符生成文本。 You can also generate the line ending there as well. 您也可以生成在那里结束的行。 The inner most loop can be used to display the total counts for that day and sales person. 最内层循环可用于显示当天和销售人员的总计数。

Although this doesn't directly answer your question, it may make answering if yourself easier. 虽然这不能直接回答您的问题,但如果您自己更容易回答,可能会有所回答。

Have you considered using objects rather than a multi-dimension array? 您是否考虑过使用对象而不是多维数组? It would make keeping track of everything a lot easier, and is general better practice for complex structures such as this one. 它可以使跟踪所有内容变得更加容易,对于像这样的复杂结构来说,这是更好的实践。 It would also allow you to abstract your calculations; 它还允许您抽象计算; such as getting the total number of products sold within a month. 例如在一个月内获得销售的产品总数。

From what I've understood, there would be 2 classes, a SalesPerson and a Product . 根据我的理解,将有2个类,一个SalesPerson和一个Product Each would 'house' the next level array of objects, and then you would simply have a single dimensional array in your main method for SalesPerson[3] . 每个都将“容纳”下一级对象数组,然后您只需在SalesPerson[3]的主方法中使用单维数组SalesPerson[3] Something like this: 像这样的东西:

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

Hope it helps. 希望能帮助到你。 :) :)

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

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