c# - 二维字符串数组中 int 值的平均数组

标签 c# arrays string int

我的程序在使用二维字符串数组保存学生姓名和成绩时遇到了一些问题。基本上,我有第二个 (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]);
       }
 }

关于c# - 二维字符串数组中 int 值的平均数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301425/

相关文章:

c - 如何转义utf-8并将utf-8代码转换为字节

c# - 如何将委托(delegate)传递给方法,其中委托(delegate)是非静态的?

arrays - 合并和返回数组

c# - 在 MS Web 窗体中实现业务逻辑的更好方法

android - 如何在 Android NDK 中比较两个 jbyte 数组

c++ - 图像卷积和边界

mysql - 在mysql字符串函数中搜索特殊字符

Excel 公式 - 将列表的子字符串匹配到列表

c# - bool 值求值的 LINQ to SQL 顺序

c# - 不允许循环文件引用。发布 ASP.NET (2.0) 网站