c#,使用.length方法

本文关键字:方法 length 使用 | 更新日期: 2023-09-27 18:05:52

我有一个方法,需要根据数组的长度进行计算。我使用.length方法进行计算,但该方法正在使用数组的最大长度(我已声明为10)进行算术。这是我用来从用户那里获取数据的循环。我知道这不是对数组数据排序的理想方式,但这是一个家庭作业,它围绕着正确使用。split方法(这不是我遇到的问题)。

for (int i = 0; i < MAX; i++)
{
    Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
    string input = Console.ReadLine();
    if (input == "")
    {
        // If nothing is entered, it will break the loop.
        break; 
    }
    // Splits the user data into 2 arrays (integer and string).
    string[] separateInput = input.Split();
    name [i] = separateInput[0];
    score [i] = int.Parse(separateInput[1]);
}

下面是我用来计算平均分的方法:

static void CalculateScores(int[] score)
{
    int sum = 0;
    int average = 0;
    for (int i = 0; i < score.Length; i++)
    {
        sum += score[i];
        average = sum / score.Length;
    }
    Console.WriteLine("The average score was {0}", average);

我像这样调用这个方法:

    CalculateScores(score);

编辑:我的数组被声明:

    int[] score = new int[MAX]; //MAX == 10.
    string[] name = new string[MAX];

CalculateScores方法是做数学好像分数。长度总是10,无论我向主机输入多少不同的分数组合。我不知道这是因为我收集输入的循环做得不正确,还是我的CalculateScores方法有缺陷。提前谢谢。

编辑:澄清,我只是困惑,为什么我不能得到正确的值从CalculateScores

c#,使用.length方法

Length始终表示数组的大小,如果您将其实例化为10,那么无论您填充了多少项,它都将始终是10。

有很多方法可以解决你的问题,但我会选择一个简单的方法,在你的计算中不使用长度,而只是将项目的数量存储在一个单独的变量中:

int numItems = 0;
for(int i=0;i<MAX;i++)
{
    Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
    string input = Console.ReadLine();
    if (input == "")
    {
        break; // if nothing is entered, it will break the loop
    }
    numItems++;
    ...
}
static void CalculateScores(int[] score, int numItems)
{
    // don't use Length at all, use numItems instead
}

数组通常用于固定大小的数据,因此Length属性反映数组可以容纳多少项,而不是数组中元素的数量。最简单的解决方法是使用List(T),它用于可变数据。

// A nice abstraction to hold the scores instead of two separate arrays.
public class ScoreKeeper
{
    public string Name { get; set; }
    public int Score { get; set; }
}
var scores = new List<ScoreKeeper>();
for (int i = 0; i < MAX; i++)
{
    Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
    string input = Console.ReadLine();
    if (input == "")
    {
        // If nothing is entered, it will break the loop.
        break; 
    }
    // Splits the user data into 2 arrays (integer and string).
    string[] separateInput = input.Split();
    scores.Add(new ScoreKeeper { Name = separateInput[0], Score = int.Parse(separateInput[1]) });
}
static void CalculateScores(ICollection<ScoreKeeper> scores)
{
    // We take advantage of Linq here by gathering all the
    // scores and taking their average.
    var average = scores.Select(s => s.Score).Average();
    Console.WriteLine("The average score was {0}", average);
}

手工检查:

int sum = 0;
int average = 0;
int length;
for (int i = 0; i < MAX; i++) {
    if(name[i]!=string.empty) {
        sum += score[i];
        length=i+1;
    }       
}
average = sum / length;
Console.WriteLine("The average score was {0}", average);