簡體   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