1

有人可以看看我的代码吗?循环时如何计算成绩,或者我可以从 StreamWriter 中取线并放入子字符串?我一直让它变得越来越混乱......我需要像这样展示它

学生得了多少分?得了B的学生得了多少?>得了C的学生有多少>得了d的学生... f...

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";
        }
    }
}
4

2 回答 2

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>
于 2013-10-04T00:54:54.917 回答
0

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

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

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

var students = new  List<Student>();

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

于 2013-10-04T00:59:12.063 回答