简体   繁体   中英

How to convert a double with an int array into a ToString override?

在此处输入图片说明

struct SSales
{
    private int[] _years;
    private double[] _sales;
    private Random salesran;

    public SSales (int [] yeararr, double[] salesarr)
    {
        //yeararr = Enumerable.Range(2000, 10).ToArray();
        _years = yeararr;
        _sales = salesarr;
        double max = 50000.00;
        double min = 1000.00;
        salesran = new Random();
        for (int i = 0; i < yeararr.Length; i++) 
        {
            for (int j = 0; j < salesarr.Length; j++)
            {
                salesarr[j] = Math.Round((salesran.NextDouble() * (max - min) + min), 2);                         
            }
        }
    }

    public override string ToString()
    {
        string itemstring; //string.join?
        return itemstring;
    }

Hi! I'm a new C# student, and I'm having a little difficulty using structures. When the generatebutton is pressed, it's supposed to call the struct SSales and generate 10 random sales numbers for each year and input them into the listbox using a ToString override as shown in the above image.

EDIT the tostring override should format like:

int[]year + "empty space" + "$" + double[]sales.

I MUST use int[] and double[].

I know I'm supposed to use string.Join, but I can't figure out how to combine two arrays of different types into a string array. Generating the array works fine, I tested it out, all I need is to know how to use the ToString override. Any help would be appreciated!

EDIT 2 I should also note that I'm also using bubble sort to organize these after generation, which will probably complicate some answers.

private void Sortbutton_Click(object sender, EventArgs e)
    {
        if (yearradio.Checked)
        {
            int temp = 0;
            for (int i = 0; i < yeararr.Length; i++)
            {
                for (int j = 0; j < yeararr.Length; j++)
                {
                    if (yeararr[i] < yeararr[j])
                    {
                        temp = yeararr[i];
                        yeararr[i] = yeararr[j];
                        yeararr[j] = temp;
                    }
                }
            }
        }

        if (salesradio.Checked)
        {
            double temp2 = 0.0;
            for (int i = 0; i < salesarr.Length; i++)
            {
                for (int j = 0; j < salesarr.Length; j++)
                {
                    if (salesarr[i] > salesarr[j])
                    {
                        temp2 = salesarr[i];
                        salesarr[i] = salesarr[j];
                        salesarr[j] = temp2;
                    }
                }
            }
        }
        //   for (int i = 0; i < yeararr.Length; i++)
        // {
        //    for (int j = 0; j < salesarr.Length; j++)
        //    {
        //        listBox1.Items.Add();
        //    }
        // }
    }

You shouldn't have the array in the struct. The struct should hold a single year's info:

struct SSales
{
    public int Year { get; private set; }
    public double Sales { get; private set; } // This should REALLY be decimal type

    public SSales(int year, double sales) : this()
    {
        Year = year;
        Sales = sales;
    }

    public override string ToString()
    {
        return string.Format("{0}    ${1:F2}", Year, Sales);
    }
}

Then you can have a single array of sales in your application:

Random salesran = new Random();
SSales[] sales = new SSales[10];
for (int i = 2000;i < 2010;++i)
    sales[i] = new SSales(i, /* random number like you have */);

Now when you add your sales array objects to your listbox, they'll be listed according to your struct's ToString() override.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM