Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

c# - Multiplying My Array Values

class Program
{
    static void Main(string[] args)
    {
        double[] values = new double[5];
        int i;
        double multValue;

        for (i = 0; i < 5; i++)
        {
            Console.Write("Enter value for number " + (i+1) + ": ");
            values[i] = double.Parse(Console.ReadLine());
        }
        Console.Write("Enter a value to multiply by: ");
        multValue = double.Parse(Console.ReadLine());

        values[1] = values[1] * multValue;
        values[2] = values[2] * multValue;
        values[3] = values[3] * multValue;
        values[4] = values[4] * multValue;
        values[5] = values[5] * multValue;

        Console.WriteLine("The new value for values[1] is: " + values[1]);
        Console.WriteLine("The new value for values[2] is: " + values[2]);
        Console.WriteLine("The new value for values[3] is: " + values[3]);
        Console.WriteLine("The new value for values[4] is: " + values[4]);
        Console.WriteLine("The new value for values[5] is: " + values[5]);

        Console.ReadLine();
    }
}

Was wondering what exactly I have to do in order to multiply each value of my array. So, for instance, if the user inputs each value as 10, 20, 30, 40, 50 and then multiplies it by 2 then I want the values of each element to be changed to 20, 40, 60, 80, 100 and then displayed. I was thinking that processing this with loops would be easier, but I'm lost as how to do it. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use the LINQ Select() extension method:

values = values.Select(d => d * multValue).ToArray();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...