2

我正在尝试开发一个简单的 EPOS 系统来记录库存和处理交易。目前我希望它使用 Streamwriter 将我的数组(产品、价格、大小、库存水平)的内容以及顶部的随机日期写入文件,但我无法破解它。我为这些数组声明的全局变量如下。

readonly static string[] Products = { "1989 Jersey", "1977 Jersey", "2001 Jersey", "1986 Jersey", "1990 Jersey", "2005 Jersey", "1992 Jersey", "1996 Jersey", "1985 Jersey", "1989 Jersey", "1991 Jersey", "1992 Jersey", "1986 Jersey"};
readonly static decimal[] Price = {26m, 24m, 21m, 25m, 19m, 22m, 23m, 18m, 16.50m, 17.50m, 14m, 20m, 17.50m};
readonly static string[] Sizes = {"Extra Small", "Small", "Medium", "Large", "Extra Large"};
    
readonly static int[,] StartingStock =     {{ 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 }};

我目前正在尝试让我的代码正常工作。我在表单加载事件中调用上述方法,它只将索引 0,0 处的内容传递给文件。

private void WriteToFileOpeningStock()
        {
            string[] Productlist = { Products[0], Sizes[0] };
            try
            {
                //enable StreamwWriter Object
                StreamWriter outputfile;

                // create file
                outputfile = File.CreateText("ProductList.txt");

                //writing array contents to file
                for (int index = 0; index < Productlist.Length; index++)
                {
                    outputfile.WriteLine(Productlist[index]);
                    outputfile.WriteLine("");
                }

                    outputfile.Close();

                MessageBox.Show("Data Entered");
            }

            catch
            {
                MessageBox.Show("Error");
            }

我试着写 string[] Productlist = { Products[0-12], Sizes[0-4] }; 让它传递所有数组内容,但它会引发异常“索引超出数组范围”。我是 C# 和编程的新手,所以非常感谢任何帮助。

4

2 回答 2

2

您正在循环通过 ProductList:

string[] Productlist = { Products[0], Sizes[0] };

里面只有 Product 的第一个索引和 Sizes 的第一个索引。所以它只会输出两个值。

您需要创建两个嵌套for循环,循环遍历ProductsSizes

于 2021-01-03T14:28:52.967 回答
1

如果你只是想列出产品,你可以做这样的事情

       try
        {
            //enable StreamwWriter Object
            StreamWriter outputfile;

            // create file
            outputfile = File.CreateText("ProductList.txt");

            //writing array contents to file
            for (int index = 0; index < Products.Length; index++)
            {
                outputfile.WriteLine(Products[index]);
                outputfile.WriteLine("");
            }

            outputfile.Close();

            MessageBox.Show("Data Entered");
        }

        catch
        {
            MessageBox.Show("Error");
        }

一般来说,我会创建一个类来保存您的产品。就像是

    public class Product
    {
        public string ProductName { get; set; }
        
        public List<string> Sizes { get; set; }
        
        public decimal Price { get; set; }
    }

然后你可以做这样的事情来转换到新的类

      Productlist = new List<Product>();
        for (int x = 0; x < Products.Length; x++)
        {
            Productlist.Add(new Product()
            {
                ProductName = Products[x],
                Price = Price[x],
                Sizes = Sizes.ToList()
            });
        }
       WriteToFileOpeningStock();

然后您可以进行以下更改以在文件中列出产品

  private void WriteToFileOpeningStock()
    {
        try
        {
            //enable StreamwWriter Object
            StreamWriter outputfile;

            // create file
            outputfile = File.CreateText("ProductList.txt");

            //writing array contents to file
            foreach(Product product in Productlist)
            {
                outputfile.WriteLine(product.ProductName);
                outputfile.WriteLine("");
            }

            outputfile.Close();

            MessageBox.Show("Data Entered");
        }

        catch
        {
            MessageBox.Show("Error");
        }
        
    }
于 2021-01-03T16:49:35.947 回答