乘以我的数组值 [英] Multiplying My Array Values

查看:102
本文介绍了乘以我的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
    }
}

想知道我到底要做什么才能乘以数组的每个值.因此,例如,如果用户输入每个值分别为10、20、30、40、50,然后将其乘以2,那么我希望将每个元素的值更改为20、40、60、80、100,然后显示.我当时以为用循环来处理它会更容易,但是我不知道该怎么做.谢谢!

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!

推荐答案

您可以使用LINQ Select()扩展方法:

You could use the LINQ Select() extension method:

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

这篇关于乘以我的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆