如何使用结构数组来使用微观的?

发布于 2025-01-20 16:22:32 字数 3529 浏览 1 评论 0原文

这段代码给我错误“至少一个对象必须实现 IComparable” 使用 Array.Sort() 时 我不知道如何使用它,如果有人提供帮助

,我会很高兴看起来你的帖子主要是代码;请添加更多详细信息。

看起来你的帖子主要是代码;请添加更多详细信息。 我的狗屎代码:

class Program
{
    struct Phones
    {
        public string PhoneName;
        public int PhonePrice;
        public int SoldAmount;
     
    }

    static void Main(string[] args)
    {
        int Sum = 30;
        int average = 0;
        Phones[] Models = new Phones[10];
        Models[0].PhoneName = "iPhone 13 Pro Max";
        Models[1].PhoneName = "Samsung Galaxy Z Flip";
        Models[2].PhoneName = "Blackview BV6300 Pro";
        Models[3].PhoneName = "Nubia Z40 Pro";
        Models[4].PhoneName = "Honor 50";
        Models[5].PhoneName = "Realme GT Master 6";
        Models[6].PhoneName = "iPhone SE";
        Models[7].PhoneName = "Nubia Z9 Max";
        Models[8].PhoneName = "Nokia N70";
        Models[9].PhoneName = "Motorola Model Z";

        Phones[] Price = new Phones[10];
        Price[0].PhonePrice = 2010;
        Price[1].PhonePrice = 1256;
        Price[2].PhonePrice = 314;
        Price[3].PhonePrice = 497;
        Price[4].PhonePrice = 477;
        Price[5].PhonePrice = 439;
        Price[6].PhonePrice = 722;
        Price[7].PhonePrice = 138;
        Price[8].PhonePrice = 30;
        Price[9].PhonePrice = 377;

        var phones = new Dictionary<int, string>();
        {
            for (int i = 0; i <= 9; i++)
            {
                phones.Add(Price[i].PhonePrice, Models[i].PhoneName);
            }
        }

        int[] PhonesSold = new int[10];
        Phones[] Sold = new Phones[10];
        Sold[0].SoldAmount = 40000000;
        Sold[1].SoldAmount = 2000000;
        Sold[2].SoldAmount = 450000;
        Sold[3].SoldAmount = 300000;
        Sold[4].SoldAmount = 1500000000;
        Sold[5].SoldAmount = 110000;
        Sold[6].SoldAmount = 60000000;
        Sold[7].SoldAmount = 290000;
        Sold[8].SoldAmount = 2500000;
        Sold[9].SoldAmount = 2000000;

        Console.WriteLine("The most populat one's:\n");
        for (int i = 0; i <= 9; i++)
        {
            if (PhonesSold[i] > 1000000)
            {
                Console.WriteLine("Phone: " + Models[i].PhoneName + " Price: " + Price[i].PhonePrice + "Sold units total(2022): " + Sold[i].SoldAmount);
            }

        }

        //less than average sold units
        for (int i = 0; i <= 9; i++)
        {
            average = average + PhonesSold[i];
        }
        average = average / 10;
        Console.WriteLine("\n Below average sold untis:\n");
        for (int i = 0; i <= 9; i++)
        {
            if (Sold[i].SoldAmount < average)
            {
                Console.WriteLine("Phone: " + Models[i].PhoneName + "Sold units total(2022): " + Sold[i].SoldAmount);
            }

        }
        Console.WriteLine("\nThe most expensive phone\n");
        for (int i = 0; i <= 9; i++)
        {
            if (Sum < Price[i].PhonePrice)
            {
                Sum = Price[i].PhonePrice;
                Console.WriteLine("Phone: " + Models[i].PhoneName + "Sold units total(2022): " + PhonesSold[i] + " Price per unit: " + Price[i].PhonePrice.ToString("C"));
            }

        }
        Console.WriteLine("\nSorted by price\n");
        Array.Sort(Price);

        for (int i = 0; i <= 9; i++)
        {
            Console.WriteLine("price: " + Price[i].PhonePrice.ToString("C") + " Model: " + phones[Price[i].PhonePrice]);
        }
    }
}

This code gives me error "At least one object must implement IComparable"
while using Array.Sort()
I have no idea how to use it and will be glad if anyone helps

It looks like your post is mostly code; please add some more details.

It looks like your post is mostly code; please add some more details.
My shit code:

class Program
{
    struct Phones
    {
        public string PhoneName;
        public int PhonePrice;
        public int SoldAmount;
     
    }

    static void Main(string[] args)
    {
        int Sum = 30;
        int average = 0;
        Phones[] Models = new Phones[10];
        Models[0].PhoneName = "iPhone 13 Pro Max";
        Models[1].PhoneName = "Samsung Galaxy Z Flip";
        Models[2].PhoneName = "Blackview BV6300 Pro";
        Models[3].PhoneName = "Nubia Z40 Pro";
        Models[4].PhoneName = "Honor 50";
        Models[5].PhoneName = "Realme GT Master 6";
        Models[6].PhoneName = "iPhone SE";
        Models[7].PhoneName = "Nubia Z9 Max";
        Models[8].PhoneName = "Nokia N70";
        Models[9].PhoneName = "Motorola Model Z";

        Phones[] Price = new Phones[10];
        Price[0].PhonePrice = 2010;
        Price[1].PhonePrice = 1256;
        Price[2].PhonePrice = 314;
        Price[3].PhonePrice = 497;
        Price[4].PhonePrice = 477;
        Price[5].PhonePrice = 439;
        Price[6].PhonePrice = 722;
        Price[7].PhonePrice = 138;
        Price[8].PhonePrice = 30;
        Price[9].PhonePrice = 377;

        var phones = new Dictionary<int, string>();
        {
            for (int i = 0; i <= 9; i++)
            {
                phones.Add(Price[i].PhonePrice, Models[i].PhoneName);
            }
        }

        int[] PhonesSold = new int[10];
        Phones[] Sold = new Phones[10];
        Sold[0].SoldAmount = 40000000;
        Sold[1].SoldAmount = 2000000;
        Sold[2].SoldAmount = 450000;
        Sold[3].SoldAmount = 300000;
        Sold[4].SoldAmount = 1500000000;
        Sold[5].SoldAmount = 110000;
        Sold[6].SoldAmount = 60000000;
        Sold[7].SoldAmount = 290000;
        Sold[8].SoldAmount = 2500000;
        Sold[9].SoldAmount = 2000000;

        Console.WriteLine("The most populat one's:\n");
        for (int i = 0; i <= 9; i++)
        {
            if (PhonesSold[i] > 1000000)
            {
                Console.WriteLine("Phone: " + Models[i].PhoneName + " Price: " + Price[i].PhonePrice + "Sold units total(2022): " + Sold[i].SoldAmount);
            }

        }

        //less than average sold units
        for (int i = 0; i <= 9; i++)
        {
            average = average + PhonesSold[i];
        }
        average = average / 10;
        Console.WriteLine("\n Below average sold untis:\n");
        for (int i = 0; i <= 9; i++)
        {
            if (Sold[i].SoldAmount < average)
            {
                Console.WriteLine("Phone: " + Models[i].PhoneName + "Sold units total(2022): " + Sold[i].SoldAmount);
            }

        }
        Console.WriteLine("\nThe most expensive phone\n");
        for (int i = 0; i <= 9; i++)
        {
            if (Sum < Price[i].PhonePrice)
            {
                Sum = Price[i].PhonePrice;
                Console.WriteLine("Phone: " + Models[i].PhoneName + "Sold units total(2022): " + PhonesSold[i] + " Price per unit: " + Price[i].PhonePrice.ToString("C"));
            }

        }
        Console.WriteLine("\nSorted by price\n");
        Array.Sort(Price);

        for (int i = 0; i <= 9; i++)
        {
            Console.WriteLine("price: " + Price[i].PhonePrice.ToString("C") + " Model: " + phones[Price[i].PhonePrice]);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文