简体   繁体   English

查找数组元素的总和

[英]Find sum of elements of an array

What calculations do I need to find the total? 我需要什么计算才能得出总数?

else if (a =2) {
    TotalCredit = new int[15];
    Console.WriteLine("please enter the credits");
    int i = 0;
    for (i = 0; i < 15; i++) {
        int Credit = Convert.ToInt32(Console.ReadLine());
        Total + Credit;         
    }

    Console.WriteLine(Total);
}

Try this. 尝试这个。

else if (a ==2)
            {
                int[] TotalCredit = new int[15];
                Console.WriteLine("please enter the credits");
                int i = 0;
                int Total = 0;
                for (i = 0; i < 15; i++)
                {
                    int Credit = Convert.ToInt32(Console.ReadLine());
                    Total += Credit;

                }

                Console.WriteLine(Total);
            }

I've added this line int Total = 0; 我将这一行添加为int Total = 0; to declare a variable Total with the value 0 , to store the total there. 声明一个值为0的变量Total ,将Total存储在那里。 Then I've changed a line in the for to be Total += Credit; 然后,我将for的一行更改for Total += Credit; which is the same as Total = Total + Credit; Total = Total + Credit; so every new value will be added and store into the variable Total . 因此,将添加每个新值并将其存储到变量Total

This is a C# I guess, as convention https://msdn.microsoft.com/en-us/library/ff926074.aspx you'd better declare the variables to be lowercase. 我猜这是C#,按照惯例https://msdn.microsoft.com/zh-cn/library/ff926074.aspx ,最好将变量声明为小写。

You need to declare the variable Total ahead it use and it should be before loop to keep its scope available after loop. 您需要在使用变量之前声明变量Total ,它应该在循环之前,以使其作用域在循环之后可用。 More than that your sum operation should be corrected using += operator Correct it as follows: 除此以外,您还可以使用+=运算符来对求和运算进行校正,如下所示:

     int Total=0;
     for (i = 0; i < 15; i++)
        {
            int Credit = Convert.ToInt32(Console.ReadLine());
            Total += Credit;

        }

        Console.WriteLine(Total);

As you have declared an array of ints I'm going to assume you want to keep the actual values the user has entered and not just the total. 当您声明了一个整数数组时,我将假设您希望保留用户输入的实际值,而不仅仅是总数。 Make sure you add System.Linq in your using clauses. 确保在using子句中添加System.Linq。

  else if (a==2)
  {
      var totalCredit = new int[15];
      Console.WriteLine("please enter the credits");
      for (int i = 0; i < 15; i++)
          totalCredit[i] = Convert.ToInt32(Console.ReadLine());

      var total = totalCredit.Sum();
      Console.WriteLine (total);
  }

A good idea would be to validate input gracefully, and minimize duplication of the magic constant 15 —even better would be to assign it a meaningful name (the same goes for the a variable). 一个好主意是优雅地验证输入,并最小化魔术常数15重复-更好的办法是为其分配一个有意义的名称( a变量也是如此)。 Also, if you intend to store each input into the array for later usage outside of the else if block, you'll need to declare it outside of said block. 同样,如果您打算将每个输入存储到数组中,以便在else if块之外使用,则需要在所述块之外声明它。 However, if you do not need the individual credit values, the array is unnecessary. 但是,如果不需要各个信用值,则不需要该数组。

const int numberOfCredits = 15;
int[] credits = new int[numberOfCredits];

... ...

else if (a == 2)
{
    int total = 0;
    int count = 0;

    while (count < numberOfCredits)
    {
        Console.WriteLine("Enter credit #" + (count + 1).ToString() + ":");

        int input;
        if (int.TryParse(Console.ReadLine(), out input))
        {
            credits[count] = input;
            total += input;
            count += 1;
        }
    }

    Console.WriteLine(total);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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