下面是我为练习一些 C# 基础和原则而编写的程序。
这个程序询问用户他们想要一个数组有多大int
,然后填充数组,最后返回数组中各个元素的平均值。
我知道我可以使用 LINQ 做到这一点,但由于我正在学习,我需要学习具体细节。
就目前而言,我编写的方法不会向控制台返回任何内容,有人可以告诉我它有什么问题吗?
在其中一个循环附近还有一条评论for
,我需要一些帮助来理解它为什么会这样。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _9_21_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter the amount of numbers you would like to find the average of: ");
int arraylength = Int32.Parse(Console.ReadLine());
int[] AverageArray = new int[arraylength];
////filling the array with user input
for (int i = 0; i < AverageArray.Length; i++)
{
Console.Write("enter the numbers you wish to find the average for: ");
AverageArray[i] = Int32.Parse(Console.ReadLine());
}
//printing out the array, without the -1 the array prints out more one number than it should, don't know why
Console.WriteLine("here is your array: ");
for (int i = 0; i < AverageArray.Length -1 ; i++)
{
Console.WriteLine(AverageArray[i]);
}
Console.WriteLine(Calcs.FindAverage(AverageArray));
Console.ReadLine();
}
}
//Method to find the average is another class for learning porpoises
class Calcs
{
public static double FindAverage(int[] averageNumbers)
{
int arraySum = 0;
for (int i = 0; i < averageNumbers.Length; i++)
arraySum += averageNumbers[i];
return arraySum / averageNumbers.Length;
}
}
}