我正在尝试创建一个程序,该程序从文本文件中获取数据并使用该文本来获取学生的成绩。我有4名学生需要输入,存储在文本文件中,这些学生按行排列:
ID ||名字||姓氏||成绩
01999911 ||比尔||盖茨|| 27,30,56,60。
我正在努力捕捉学生的成绩,这些成绩大约为27 (earned points),30 (possible points),56 (earned points),60 (possible points)
。该字符串扩展为获得积分的10个不同值,以及可能积分的10个值,以获得点的格式,后跟可能的点,以逗号分隔。之后,我必须获取这些数据点并找到学生的平均成绩。
我想知道是否有人能够指出我可以学习如何编写代码的方向,或提供一些示例代码以获得帮助。感谢您的回复。
答案 0 :(得分:1)
我假设在问题60之后的句号是一个错字。我假设'成绩'指的是赚取/可能。
由于您已经说过要编写代码来填充列表,我建议使用制表符而不是两个管道字符分隔符。这将使文件成为TSV文件,并使代码稍微更容易编写。 (标签字符在源代码中由\t
表示,可以是字符串,也可以是字符:'\t'
。)如果您自己编写文件,则有助于保持获得的& amp ;可能的值一起使用不同的分隔符;例如27,30;56,60;45,70
(等)。这使得分离更容易;但我会为两者展示解决方案。
public class Result
{
// This constructor is used by the "different separator for Earned and Possible" approach.
// Remove this and the default constructor if you don't want to go that way.
public Result(string line)
{
var parts = line.Split(',');
if (parts.Length != 2)
{
throw new ArgumentException($"Result had {parts.Length} parts: '{line}'");
}
EarnedPoints = int.Parse(parts[0]);
PossiblePoints = int.Parse(parts[1]);
}
public Result()
{
}
// Remove this constructor if you go for the approach above.
public Result(int earnedPoints, int possiblePoints)
{
EarnedPoints = earnedPoints;
PossiblePoints = possiblePoints;
}
public int EarnedPoints { get; }
public int PossiblePoints { get; }
public double Grade => (double)EarnedPoints / PossiblePoints;
public string GradeAsPercentage => $"{Grade:P2}";
}
public class Student
{
public Student(string line)
{
var parts = line.Split(new[] { "||" }, StringSplitOptions.None);
// Could use this instead if you go for TSV: var parts = line.Split('\t');
if (parts.Length != 4)
{
throw new ArgumentException($"Student had {parts.Length} parts: '{line}'");
}
Id = parts[0];
Forenames = parts[1];
Surname = parts[2];
string remainder = parts[3];
// This is how simple it would be if you used a different separator to group the Earned and Possible together
// Results = remainder.Split(';').Select(result => new Result(result)).ToList();
// Below is an approach where commas are used for everything...
Results = new List<Result>();
var items = remainder.Split(',');
if (items.Length % 2 == 1)
{
throw new ArgumentException($"Odd number of parts in the result: '{line}'");
}
int offset = 0;
while (offset < items.Length)
{
int earnedPoints = int.Parse(items[offset++]);
int possiblePoints = int.Parse(items[offset++]);
Results.Add(new Result(earnedPoints, possiblePoints));
}
}
public string Id { get; } // Could make this an 'int', in which case use "int.Parse(parts[0])" when setting it, above
public string Forenames { get; }
public string Surname { get; }
public IList<Result> Results { get; }
public double AverageGrade => Results.Average(r => r.Grade);
public string AverageGradeAsPercentage => $"{AverageGrade:P2}";
}
然后你可以这样做来阅读整个文件...
var students = File.ReadLines(yourFileName).Skip(1).Select(l => new Student(l));
小心谨慎:我得走了,所以匆匆一点,但希望一切顺利。
答案 1 :(得分:-1)
一种方法是:
您可以将文本文件中的文本读入字符串
找到&#39;的第一个索引,&#39; (假设你的文件没有 有无关的逗号)
然后你可以子串(切)原始字符串
如果索引减去3是数字
,则从逗号的第一个索引减去3如果索引减去2是数字
,则从逗号的第一个索引减去2否则从逗号的第一个索引减1(感谢Richardissimo)
通过&#39;,&#39;
在进行数学运算时迭代结果数组
从手机发布。对不起打字错误。 :)