使用方法对数组的值求和

Sum the values of the array with a method

所以我试图通过控制台从用户那里获取 10 个输入,将它们存储在一个数组中,用一种方法计算总和,然后 return 它。我还在这个过程中,所以这不是完整的代码,但我现在很困惑为什么我的错误列表说 Main 应该在 while 语句之后关闭?

    class Program
{//Start class

    //We need to declare the size of our array
    const int ARRAYSIZE = 10;


    static void Main()
    {//Start main

        //We need to create a counter for how many entries we currently have
        int entries = 0;
        int sumScore = 0;

        //We need to create the array 
        int[] myArray = new int[ARRAYSIZE];

        //Start a loop to ask for input
        do
        {//Start loop
            Console.Write("Please enter the score of each test: ");
            int entry = int.Parse(Console.ReadLine());
            myArray[entries] = entry;
            entries++;

            }
            while (entries < ARRAYSIZE);//End loop

           static void PrintArray(int[ ] myArray)
            {
                foreach(int value in myArray)
             {
                Console.WriteLine(value);
                Console.ReadLine();
             }
           }
    }//End main
}//End class

static void PrintArray(int[ ] myArray) 的位置,您正在声明一个新函数。在声明新函数之前需要 }//End main

   do
    {//Start loop
        Console.Write("Please enter the score of each test: ");
        int entry = int.Parse(Console.ReadLine());
        myArray[entries] = entry;
        entries++;

        }
        while (entries < ARRAYSIZE);//End loop
}//End main
       static void PrintArray(int[ ] myArray)
        {
            foreach(int value in myArray)
         {
            Console.WriteLine(value);
            Console.ReadLine();
         }
       }

您放错了 Main 方法的右括号。您可能还想在 while (entries < ARRAYSIZE);//End loop 之后调用 PrintArray 方法,并在该方法中计算总和。但我想这是因为,正如你所说,这是一项正在进行的工作。这是它的样子

 class Program
{
    const int ARRAYSIZE = 10;

    static void Main()
    {//Start main

        //We need to create a counter for how many entries we currently have
        int entries = 0;
        int sumScore = 0;

        //We need to create the array 
        int[] myArray = new int[ARRAYSIZE];

        //Start a loop to ask for input
        do
        {//Start loop
            Console.Write("Please enter the score of each test: ");
            int entry = int.Parse(Console.ReadLine());
            myArray[entries] = entry;
            entries++;

        }
        while (entries < ARRAYSIZE);//End loop
        PrintArray(myArray);

      }//End main

      static void PrintArray(int[ ] myArray)
      {
            int sum = 0;
            foreach(int value in myArray)
            {
                 sum += value;
                 Console.WriteLine(value);
                 Console.ReadLine();
            }
            Console.WriteLine(sum);
       }

}

这不是您问题的直接答案,但我认为您可能会感兴趣。

如果您想以更简单的方式执行此操作,请尝试以下操作:

static void Main()
{
    var sum =
        Enumerable
            .Range(0, ARRAYSIZE)
            .Select(n =>
            {
                Console.WriteLine("Please enter the score of each test:");
                return int.Parse(Console.ReadLine());
            })
            .Sum();

    Console.WriteLine();
    Console.WriteLine("The sum is:");
    Console.WriteLine(sum);
}