我的程序存在一些问题,该程序在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("");
}
似乎平均数组没有正确填充。如果我删除平均分区,它似乎只打印出1,2,3,4,5,6,7。此外,如果我将平均值的长度更改为5 - 或者名称数量的长度,因为只有5个值,它会中断。这可能是因为它试图将7个项目添加到5个项目数组中。
答案 0 :(得分:2)
每个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]);
}
}
略过第一个元素
Numeric
答案 1 :(得分:1)
您尝试在平均值中包含名称字符串(在0索引上)。例如导致异常的for (int j = 1; j < studentGrades.GetLength(1); j++)
,只需跳过每个列上的第一个元素。
{{1}}
答案 2 :(得分:1)
您必须更改此部分代码:
// 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]);
}
答案 3 :(得分:1)
正如其他人正确指出的那样,您需要在以下行中将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();