简体   繁体   中英

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; to declare a variable Total with the value 0 , to store the total there. Then I've changed a line in the for to be Total += Credit; which is the same as Total = Total + Credit; so every new value will be added and store into the variable 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.

You need to declare the variable Total ahead it use and it should be before loop to keep its scope available after loop. 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.

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

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