C# 从用户输入的数组中获取平均值
嗨,我正在尝试制作一个程序,要求用户输入最多25个数字,然后显示它们的平均值,但当它要求数字时,它只是通过ans一次又一次地要求数字,而不显示平均值 任何指导都将不胜感激C# 从用户输入的数组中获取平均值,c#,C#,嗨,我正在尝试制作一个程序,要求用户输入最多25个数字,然后显示它们的平均值,但当它要求数字时,它只是通过ans一次又一次地要求数字,而不显示平均值 任何指导都将不胜感激 static void Main(string[] args) { Console.WriteLine("enter the amount of numbers you would like to find the average and mean of: "); int arrayl
static void Main(string[] args)
{
Console.WriteLine("enter the amount of numbers you would like to find the average and mean of: ");
int arraylength = Int32.Parse(Console.ReadLine());
int[] AverageArray = new int[25];
//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
Console.WriteLine("here is your array: ");
for (int i = 0; i < AverageArray.Length; i++)
{
Console.WriteLine(AverageArray[i]);
}
Console.WriteLine(FindAverage(AverageArray));
}
public static double FindAverage(int[] averageNumbers)
{
int arraySum = 0;
for (int i = 0; i < averageNumbers.Length; i++)
arraySum += averageNumbers[i];
return arraySum / averageNumbers.Length;
}
}
static void Main(字符串[]args)
{
Console.WriteLine(“输入您希望查找的平均数和平均数:”);
int-arraylength=Int32.Parse(Console.ReadLine());
int[]AverageArray=新int[25];
//用用户输入填充数组
for(int i=0;i
既然你已经有了阵列长度
,为什么不把它用作你的阵列大小指示器呢?此外,你的平均例行程序将失去精度。您返回的是double
,但您的代码的所有内容都是int
static void Main(string[] args)
{
Console.WriteLine("enter the amount of numbers you would like to find the average and mean of: ");
int arraylength = Int32.Parse(Console.ReadLine());
int[] AverageArray = new int[arraylength];
//filling the array with user input
for (int i = 0; i < arraylength; i++)
{
Console.Write("enter the numbers you wish to find the average for: ");
AverageArray[i] = Int32.Parse(Console.ReadLine());
}
//printing out the array
Console.WriteLine("here is your array: ");
for (int i = 0; i < arrayLength; i++)
{
Console.WriteLine(AverageArray[i]);
}
Console.WriteLine(FindAverage(AverageArray));
}
public static double FindAverage(int[] averageNumbers)
{
// change to 'double' otherwise you will lose precision
double arraySum = 0;
for (int i = 0; i < averageNumbers.Length; i++)
arraySum += averageNumbers[i];
return arraySum / averageNumbers.Length;
}
static void Main(字符串[]args)
{
Console.WriteLine(“输入您希望查找的平均数和平均数:”);
int-arraylength=Int32.Parse(Console.ReadLine());
int[]AverageArray=新的int[arraylength];
//用用户输入填充数组
对于(int i=0;i
首先,应使用数组长度初始化AverageArry,以便int[]AverageArray=new int[arraylength];然后,它将循环通过与你的长度相等的次数,每次请求一个数字。你是说它永远不会把这件事留在循环中吗?事实上是逐字逐句@克里特纳:问题似乎出在他的圈套上,而不是实际的发现average@Kritner哈哈,天哪,我刚看了你的链接线程XD,这是一个完全复制粘贴:P.@psoshmo,我以为我在矩阵中呆了一会儿。据我所知,我之前评论过一个非常类似的问题。