从对象列表中选择一个随机对象

时间:2020-02-10 03:56:13

标签: c# random

我正在尝试从列表中随机选择一个客户项目。我不确定该如何实际打印出列表中的信息。

我将此作为客户类别

namespace PizzaParlor
{
    class Customer
    {
        private string name;
        private int flavor;
        private int price;
        private int quality;
        private int speed;
        private int accessibility;
        private int brand;
        private int convenience;
        private int variety;

        public Customer(string name, int flavor, int price, int quality, int speed, int accessibility, int brand, int convenience, int variety)
        {
            this.name = name;
            this.flavor = flavor;
            this.price = price;
            this.quality = quality;
            this.speed = speed;
            this.accessibility = accessibility;
            this.brand = brand;
            this.convenience = convenience;
            this.variety = variety;
        }

        // Name, Speed, Quality, Variety, Convenience, Accessibility, price, brand, flavor
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public int Quality
        {
            get { return quality; }
            set { quality = value; }
        }

        public int Variety
        {
            get { return variety; }
            set { variety = value; }
        }

        public int Convenience
        {
            get { return convenience; }
            set { convenience = value; }
        }

        public int Accessibility
        {
            get { return accessibility; }
            set { accessibility = value; }
        }

        public int Price
        {
            get { return price; }
            set { price = value; }
        }

        public int Brand
        {
            get { return brand; }
            set { brand = value; }
        }

        public int Flavor
        {
            get { return flavor; }
            set { flavor = value; }
        }
    }
}

这是我设置为与客户类一起使用的主要类:

namespace PizzaParlor 
{
    class Program
    {
        static void Main(string[] args)
        {
            var random = new Random();

            List<Customer> CustomerList = new List<Customer>();

            CustomerList.Add(new Customer("bill", 20,15,10,5,10,20,5,15));
            CustomerList.Add(new Customer("kevin", 15, 10, 5, 20, 15, 15, 0, 20));
            CustomerList.Add(new Customer("clair", 8,25,2,25,5,15,0,20));
            CustomerList.Add(new Customer("jim", 15,20,10,15,0,40,0,0));
            CustomerList.Add(new Customer("rachel", 20,15,10,5,10,30,0,10));
            CustomerList.Add(new Customer("jeff", 30,20,5,5,10,10,0,20));
            CustomerList.Add(new Customer("Mike", 21,23,0,10,14,16,0,16));
            CustomerList.Add(new Customer("john", 25,15,10,10,10,5,5,20));

            int index = random.Next(CustomerList.Count);

            Console.WriteLine(CustomerList[index]);
        }
    }
}

我知道random.Next(CustomerList.Count)将从列表中随机选择一个,但是我不知道为什么它返回此输出:

output picture

2 个答案:

答案 0 :(得分:1)

这是因为,当您尝试打印对象(例如Customer时,将执行ToString()的默认实现。这会产生您看到的输出。

有2种修复方法

  1. 打印您感兴趣的显式字段
int index = random.Next(CustomerList.Count);
var customer = CustomerList[index];
Console.WriteLine($"customer name = {customer.Name}, flavour = {customer.Flavour}}");
  1. 覆盖ToString实现
class Customer 
{
   //...
   // Existing code
   // ..


   public override string ToString ()
   {
       return $"customer name = {customer.Name}, flavour = {customer.Flavour}}";
   } 
}

在您的主要方法中

int index = random.Next(CustomerList.Count);
var customer = CustomerList[index];
Console.WriteLine(customer);

答案 1 :(得分:0)

您可以使用Reflection实现此目的。

 foreach (var prop in typeof(Customer).GetProperties())
            {
                Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(CustomerList[index]));
            }