通过在streamwriter中使用答案循环计数?

时间:2013-10-04 00:47:35

标签: c# loops streamwriter

有人可以查看我的代码吗?如何在循环时计算等级,或者我可以从StreamWriter获取行并放置子串? 我一直把它变得越来越乱......我需要像

一样显示它

得分A的学生有多少? 得到B的学生有多少?> 得到C多少的学生> 得到的学生... ... ...

using (StreamReader reader = new StreamReader("Student.txt"))
{
    StreamWriter output = new StreamWriter("Result.txt");
    String line;
    line = reader.ReadLine();

    while (line != null)
    {

        String name = line.Substring(0, 9);
        String Asg1 = line.Substring(10, 3);
        String Asg2 = line.Substring(16, 3);
        String Test = line.Substring(22, 3);
        String Quiz = line.Substring(28, 3);
        String Exam = line.Substring(34, 3);

        int intAsg1 = int.Parse(Asg1);
        int intAsg2 = int.Parse(Asg2);
        int intTest = int.Parse(Test);
        int intQuiz = int.Parse(Quiz);
        int intExam = int.Parse(Exam);

        int percentAsg1 = (intAsg1 * 25) / 100;
        int percentAsg2 = (intAsg2 * 25) / 100;
        int percentTest = (intTest * 10) / 100;
        int percentQuiz = (intQuiz * 10) / 100;
        int percentExam = (intExam * 30) / 100;


        // this part i dont get it~ 
        **String Grade;
        if (overallScore >= 70)
        {
            Grade = "A";
        }
        else if (overallScore >= 60)
        {
            Grade = "B";
        }
        else if (overallScore >= 50)
        {
            Grade = "C";
        }
        else if (overallScore >= 40)
        {
            Grade = "D";
        }
        else
        {
            Grade = "F";
        }
    }
}

2 个答案:

答案 0 :(得分:1)

它没有循环。

while !reader.EndOfStream
{
  /*now check each Readline.
    parse each line and create a new grade object for 
    each iteration and add to collection*/
}

为每个学生存储数据的自定义类:

public class grade
{
 public int asg1{ get; set; }
 public int asg2{ get; set; }
 public int test{ get; set; }
 public int quiz{ get; set; }
 public int exam{ get; set; }
 //whatever else you need
}

成绩集合:

var grades = New List<grade>

答案 1 :(得分:0)

我认为除了纠正readline循环之外,你正在寻找一个保持结果的类。

public class Student
{
public string Name { get; set; }
public List<Grade> Grades { get; set; }
}

然后在开始循环之前创建一个列表

var students = new  List<Student>();

然后在每次迭代中添加到列表中