首页 > 解决方案 > 数组的错误元素被填充

问题描述

我需要你的帮助,

我正在向 4 个单个数组添加一些值,当用户输入相同的产品 ID 超过 1 次时,它只会在找到 ID 的确切位置修改其中一个数组(cantidad_prod)。

我为 ID 1 运行了两次“for”循环,第一次它会问我所有的细节,第二次它只会问我新的数量(如果 ID 1 在位置 0,它会修改位置 0 上的数量数组)。

由于我只更新位置 0 ,每个数组上应该只有 1 个元素,但屏幕显示 2 个元素。我究竟做错了什么?以下是我的代码,提前致谢:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exerc_6
{
    class Program
    {
        static void Main(string[] args)
        {
            int total_prod, i, ID,TextIndex,total_elements;
            double total_a_pagar=0;
            //Input 2 for testing
            Console.WriteLine("Products being purchased?");
            total_prod = int.Parse(Console.ReadLine());
            Console.Clear();

            int[] ID_Prod = new int[total_prod];
            string[] Nombre_Producto = new string[total_prod];
            int[] cantidad_prod = new int[total_prod];
            double[] precio_uni = new double[total_prod];


            for (i = 0; i < total_prod; i++)
            {

                Console.WriteLine("Add the product ID");
                ID = int.Parse(Console.ReadLine());
                TextIndex = Array.FindIndex(ID_Prod, m => m == ID);
                Console.Clear();

                if (TextIndex>-1)
                {
                    Console.WriteLine("Amount of Products for ID #"+ID);
                    cantidad_prod[TextIndex] = cantidad_prod[TextIndex] + int.Parse(Console.ReadLine());
                    Console.Clear();
                }

                else

                {
                    ID_Prod[i] = ID;
                    Console.WriteLine("ProductName for  ID #"+ID);
                    Nombre_Producto[i] = Console.ReadLine();
                    Console.WriteLine("Amount of Products for ID ID #" + ID);
                    cantidad_prod[i] = cantidad_prod[i] + int.Parse(Console.ReadLine());
                    Console.WriteLine("Unit Price for ID #" + ID);
                    precio_uni[i] = cantidad_prod[i] + Double.Parse(Console.ReadLine());
                    Console.Clear();
                }

            }

            //This must be 1 if we enter #1 twice
            total_elements = ID_Prod.Length;
            Console.WriteLine(total_elements);

            Console.ReadKey();
        } } }

标签: c#

解决方案


您的问题是您正在创建一个大小为 2 的固定长度数组,然后只修改第一个索引两次。一个数组可以包含一个空索引,当你.Count在一个大小数组上调用时,尽管一个索引是空的,它还是会返回 2。

也许看看创建一个对象并将这些值存储为属性,或者查看使用列表并保持现有for循环迭代 total_prod 并使用该List.Add(...)方法将任何新 ID 和值附加到 4 个列表(是数组)

List<int> ID_Prod = new List<int>();
List<string> Nombre_Producto = new List<string>;
List<int> cantidad_prod = new  List<int>;
List<double> precio_uni = new List<double>;

For(int i = 0; i < total_prod; i++)
{
    //some code
    int ID = int.Parse(Console.ReadLine());
    If(!ID_Prod.Contains(ID))
    {
        ID_Prod.Add(ID);
        //append other lists
    }
        //update lists
    }
 }

创建自己的对象时:

Class Product {
    public int ID {get; set;}
    public string Name {get; set;}
    public int Quantity {get; set;}
    public double Price {get; set;} 

    public Product(int ID, string Name, int Quantity, double Price) 
    {
        this.ID = ID;
        this.Name = Name;
        this.Quantity = Quantity;
        this.Price = Price;
     }
 }

然后在您的代码中:

 List<Product> products = new List<Product>();
 //... get values from user
Product p = new Product(ID, Name, Quantity, Price);
 products.Add(p);

并找到要编辑的产品:

 //user enters ID
 int ID = int.Parse(Console.ReadLine());
  If(products.Any(x=>x.ID == ID))
  {
       Product p = products.Where(x=>x.ID==ID).FirstOrDefault();
  //modify p and this will update in list as it is a reference type
  }

推荐阅读