用于乘以数组元素的函数

时间:2014-02-04 06:42:24

标签: c# arrays multiplication

我正在做一个计算器,这是代码:

如果我进入乘法功能并输入例如我想要多个2 * 2 * 2 这段代码输出4 8 4 我不明白为什么,我知道2 * 2 = 4 * 2 = 8但为什么最后4? 如何才能在没有获得完整系列的情况下获得结果?当我尝试在for循环之外执行Console WriteLine时会抛出错误。它不会让我。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static void LetsSum(int [] myArray)
        {
            int sum = myArray.Sum();
            Console.WriteLine(sum);     
        }

        static void  letsMult(int [] myArray)
        {  
            for (int a = 0; a < myArray.Length; a++ )
            {
                for (int b = a+1; b < myArray.Length; b++ )
                {
                int multip =  myArray[a] *= myArray[b];
                Console.WriteLine(multip);

                }

            }

        }

        static void Main(string[] args)
        {

            //First Variable total of numbers from users input
            int TotalNumb = 0;           
            //String to receive users input
            string TotalN = string.Empty;
            //Specifying size of array
            Console.WriteLine("Please specify how many numbers do you want to do the math");
            TotalN = Console.ReadLine();
            //Converte because readline it's a string
            TotalNumb = Convert.ToInt32(TotalN);
            //Name of ARRAY and passing the value selected by the user
            int[] myArray = new int[TotalNumb];

            //counter for the loop
            int i = 0;
                    for (i = 0; i < TotalNumb; i++ )
                    {
                        Console.WriteLine("Enter your number");

                        myArray[i] = Convert.ToInt32(Console.ReadLine());
                    }

            Console.WriteLine("Please enter SUM, LESS, MULT, DIV");
            string ToDo = Console.ReadLine();


                    if (ToDo == "SUM")
                    {
                        Program.LetsSum(myArray);
                    }

                    if (ToDo == "MULT")
                    {
                        Program.letsMult(myArray);
                    }

                    Console.Read();
        }
    }
}

6 个答案:

答案 0 :(得分:0)

错误消息是因为您在for循环的最内层范围内声明int multip。之后int超出范围,并且不可用。如果您希望multip可以在for循环之外访问,请在for循环之外声明它:

    static void  letsMult(int [] myArray)
    {  
        int multip = 1;
        for (int a = 0; a < myArray.Length; a++ )
        {
            for (int b = a+1; b < myArray.Length; b++ )
            {
              multip =  myArray[a] *= myArray[b];
            }

        }
        Console.WriteLine(multip);
    }

注意,它在for循环上方声明,但在循环中使用,并在循环外输出,但仍在其范围内(在我的情况下是“整个功能“)。

您仍有逻辑问题。你确定需要一个嵌套循环吗?您如何向一个人描述如何将数字列表相乘以获得总产品?

答案 1 :(得分:0)

非常简单......改变你的代码就像吼叫......

 static void  letsMult(int [] myArray)
        {  
            int output=1;

            for (int a = 0; a < myArray.Length; a++ )
            {
                  output=output*myArray[a];
                  Console.WriteLine(output);
            }    
        }

答案 2 :(得分:0)

数组中有3个元素

  

[2,2,2]

然后乘以0和1并输出结果:

  

[4,2,2]输出:4

然后乘以0和2并输出结果:

  

[8,2,2]输出:8

n乘以1和2并输出结果:

  

[8,4,2]输出:8

也许您想要删除外部循环,在这种情况下,您的输出将是

  

4,8

答案 3 :(得分:0)

使用

int multip=1;

for (int a = 0; a < myArray.Length; a++ )
            {

                 multip  *= myArray[a];
                Console.WriteLine(multip);

                }

            }

答案 4 :(得分:0)

您可以使用LINQ的Aggregate()

执行此操作

总和:

  int[] arr1 = new int[] { 2, 2, 2 };
  int result = arr1.Aggregate((a, b) => b + a);
 //or
  int result = arr1.Sum();

用于乘法:

  int[] arr1 = new int[] { 2, 2, 2 };
  int result = arr1.Aggregate((a, b) => b * a);

答案 5 :(得分:0)

我想你想显示每个乘法结果。如果你这样做,你可以做到以下几点:

int multip = myArray[0];
for (int a = 1; a < myArray.Length; a++ )
{
    Console.Write("{0} * {1} = ",multip, myArray[a]);
    multip *= myArray[a];
    Console.WriteLine(multip);
}