簡體   English   中英

2D字符串數組中int值的平均數組

[英]Average Array for int values in a 2D String Array

我的程序存在麻煩,該程序將學生姓名和成績保存在2D字符串數組中。 基本上,我有第二個(1D)數組,可以容納每個學生的平均成績。

到目前為止的代碼:

class Program
{
    static void Main(string[] args)
    {
        // Create a 2D array for names, and their grades, and an array for average grades
        string[,] studentGrades = new string[5, 8];
        int[] average = new int[studentGrades.GetLength(0)];

        // Set student names/grades manually
        studentGrades[0, 0] = "Steve";
        studentGrades[0, 1] = "69";
        studentGrades[0, 2] = "80";
        studentGrades[0, 3] = "66";
        studentGrades[0, 4] = "75";
        studentGrades[0, 5] = "90";
        studentGrades[0, 6] = "69";
        studentGrades[0, 7] = "98";

        studentGrades[1, 0] = "Bob";
        studentGrades[1, 1] = "73";
        studentGrades[1, 2] = "67";
        studentGrades[1, 3] = "65";
        studentGrades[1, 4] = "91";
        studentGrades[1, 5] = "48";
        studentGrades[1, 6] = "33";
        studentGrades[1, 7] = "94";

        studentGrades[2, 0] = "Lewis";
        studentGrades[2, 1] = "67";
        studentGrades[2, 2] = "80";
        studentGrades[2, 3] = "66";
        studentGrades[2, 4] = "75";
        studentGrades[2, 5] = "90";
        studentGrades[2, 6] = "69";
        studentGrades[2, 7] = "63";

        studentGrades[3, 0] = "Sara";
        studentGrades[3, 1] = "55";
        studentGrades[3, 2] = "58";
        studentGrades[3, 3] = "63";
        studentGrades[3, 4] = "70";
        studentGrades[3, 5] = "55";
        studentGrades[3, 6] = "55";
        studentGrades[3, 7] = "76";

        studentGrades[4, 0] = "Xavier";
        studentGrades[4, 1] = "22";
        studentGrades[4, 2] = "27";
        studentGrades[4, 3] = "25";
        studentGrades[4, 4] = "19";
        studentGrades[4, 5] = "42";
        studentGrades[4, 6] = "18";
        studentGrades[4, 7] = "32";

        // Loop the array dimensions and output/format output the names/grades
        for (int name = 0; name < studentGrades.GetLength(0); name++)
        {
            for (int grade = 0; grade < studentGrades.GetLength(1); grade++)
            {
                Console.WriteLine(studentGrades[name, grade]);
            }
            Console.WriteLine("");
        }

        for (int i = 0; i < studentGrades.GetLength(0); i++)
        {
            for (int j = 0; j < studentGrades.GetLength(1); j++)
            {
                average[j] += int.Parse(studentGrades[i, j]);
            }
        }

    }
}

關於輸入字符串不是設置平均值的正確格式,我得到了一個未處理的異常。

感謝所有幫助!

更新資料

到目前為止,我已經回顧了代碼的解決方案,但仍然存在使平均成績按預期工作的問題。 我有5個7年級的學生。 我希望該程序在他們的欄下輸出每個人的平均成績。

碼:

static void Main(string[] args)
    {
        // Create a 2D array for names, and their grades, and an array for average grades
        string[,] studentGrades = new string[5, 8];
        int[] average = new int[studentGrades.GetLength(1)];

        // Set student names/grades manually
        //As done above, excluded for brevity reasons


        // Loop the array dimensions and output/format output the names/grades
        for (int grade = 0; grade < studentGrades.GetLength(1); grade++)
        {
            for (int name = 0; name < studentGrades.GetLength(0); name++)
            {
                // Composite formatting is used to align names/grades in grid -- specifically the alignment component.
                // Making the value higher (more neg) will increase spacing between elements
                // Positive number would right align elements instead
                Console.Write("{0, -15}", studentGrades[name, grade]);
            }
            // Moves each printed grade to a new line
            Console.WriteLine("");
        }

        for (int i = 0; i < studentGrades.GetLength(0); i++)
        {
            for (int j = 1; j < studentGrades.GetLength(1); j++)
            {
                average[j] += int.Parse(studentGrades[i, j]);
            }

            average[i] /= studentGrades.GetLength(1) - 1;
        }

        for (int i = 0; i <= average.GetLength(0) - 1; i++)
        {
            Console.Write("{0, -15}", i);
        }
        Console.WriteLine("");


    }

更新2

似乎平均數組未正確填充。 如果我刪除平均除法,似乎只打印出1、2、3、4、5、6、7。 此外,如果我將平均值的長度更改為5或名稱數量的長度,因為只有5個值,則會中斷。 這可能是因為它試圖將7個項目添加到5個項目的數組中。

每個studentGrages[*, 0]元素都不是整數,因此無法將其解析為int

跳過第一個元素

 for (int i = 0; i < studentGrades.GetLength(0); i++)
 {
       for (int j = 1; j < studentGrades.GetLength(1); j++)
       {
                average[j] += int.Parse(studentGrades[i, j]);
       }
 }

您試圖將名稱的字符串(在0索引上)包括在平均值中。 例如int.Parse("Steve")會導致異常,只需跳過每個列的第一個元素。

for (int j = 1; j < studentGrades.GetLength(1); j++)

您必須更改這部分代碼:

// Store the average as a double array, otherwise you can't have averages with decimal values.
double[] average = new double[studentGrades.GetLength(0)];

for (int i = 0; i < studentGrades.GetLength(0); i++)
{
    // Start from j = 1, as j = 0 would get a name instead of a grade.
    for (int j = 1; j < studentGrades.GetLength(1); j++)
    {
        // Here use average[i] instead of average[j], because you want
        // the average grade for each student.
        average[i] += int.Parse(studentGrades[i, j]);
    }

    // Finish calculating the average.
    // GetLength(0) - 1 because the first item is a name, not a grade.
    average[i] /= studentGrades.GetLength(1) - 1;
}

for (int i = 0; i <= average.GetLength(0) - 1; i++)
{
    // Show at most 2 decimal digits.
    Console.Write("{0, -15:#.##}", average[i]);
}

正如其他人正確指出的那樣,您需要在以下行中將0更改為1

for (int j = 1; j < studentGrades.GetLength(1); j++)

否則,您將嘗試將名稱解析為整數。

另外,您需要將循環內的平均索引更改為i而不是j

average[i] += int.Parse(studentGrades[i, j]);

您將要遇到的另一個問題是您正在使用整數數學-因此,當您執行類似7 / 2 ,得到的答案是3而不是3.5

如果我是您,並且您不想更改數據結構,則可以這樣操作:

var results =
    studentGrades
        .Cast<string>()
        .Select((x, n) => new { x, n })
        .GroupBy(xn =>  xn.n / studentGrades.GetLength(1), xn => xn.x)
        .Select(xs => new
        {
            student = xs.First(),
            average = xs.Skip(1).Select(x => int.Parse(x)).Average(),
        })
        .ToArray();

這給了我:

結果

另外,我建議您將數據結構更改為此:

var studentGrades = new []
{
    new { student = "Steve", grades = new [] { 69, 80, 66, 75, 90, 69, 98, } },
    new { student = "Bob", grades = new [] { 73, 67, 65, 91, 48, 33, 94, } },
    new { student = "Lewis", grades = new [] { 67, 80, 66, 75, 90, 69, 63, } },
    new { student = "Sara", grades = new [] { 55, 58, 63, 70, 55, 55, 76, } },
    new { student = "Xavier", grades = new [] { 22, 27, 25, 19, 42, 18, 32, } },
};

然后,這應該為您工作:

var results =
    studentGrades
        .Select(sg => new
        {
            sg.student,
            average = sg.grades.Average(),
        })
        .ToArray();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM