循环参数参数

问题描述:

我正在完成一项家庭作业任务,并且遇到了一个循环遍历函数的小问题,以便为文件中的每个名称返回参数。我希望有人能够很快看到这一点,并建议我应该在哪里放置一个循环。我在任何地方都尝试过,这对我来说很有意义。简要介绍该功能的工作原理。它会查看一个包含多个学生的档案,分数为&小时,然后为每个学生计算GPA。将学生姓名和GPA返回给Main,然后调用另一个函数来创建输出文件。目前创建的唯一文件是文件中的最终学生(他的GPA的Miguel Ortiz &)。循环参数参数

对不起,这段代码太难看了。示例文件很荒谬!

输入文件看起来是这样的:

Jack Johnson 
A 
2 
C 
3 
F 
2 
D 
2 
B 
4 
A 
4 
Miguel Ortiz 
C 
4 
A 
3 
F 
2 

代码写成:

public static void ProcessGrades(ref string File, out string Name, out double GPA) 
    { 
    string FilePath = @"C:\Users\heathi\Documents\Visual Studio 2013\Projects\" + 
     @"GPA Calculation\GPA Calculation\bin\Release\"; 
     StreamReader inFile = new StreamReader(FilePath + File); 
    double gpa = 0.0; 
    string letterGrade = ""; 
    string line; 
    double grade = 0; 
    double hours = 0; 
    double points = 0; 
    double i = 0; 
    bool result = false; 
    string studentName = ""; 

    while ((line = inFile.ReadLine()) != null) 
    { 
     if (line.Length > 1) 
     { 
     if (studentName != line) 
     { 
      if (hours != 0) 
      { 
      gpa = (points/hours); 
      gpa = Math.Round(gpa, 2); 
      Console.WriteLine(studentName + " GPA " + gpa.ToString()); 
      } 
      studentName = line; 
      points = 0; 
      hours = 0; 
      grade = 0; 
     } 
     } 
     else 
     { 
     result = double.TryParse(line, out i); 
     if (result == true) 
     { 
      hours += double.Parse(line); 
      points += (grade * double.Parse(line)); 
     } 
     else 
     { 
      letterGrade = line; 

      switch (letterGrade) 
      { 
      case "A": 
       grade = 4; 
       break; 
      case "B": 
       grade = 3; 
       break; 
      case "C": 
       grade = 2; 
       break; 
      case "D": 
       grade = 1; 
       break; 
      case "F": 
       grade = 0; 
       break; 
      } 
     } 
     } 
    } 
    gpa = (points/hours); 
    gpa = Math.Round(gpa, 2); 
    Console.WriteLine(studentName + " GPA " + gpa.ToString()); 

    Name = studentName; 
    GPA = gpa; 
    } // end ProcessGrades 
+0

与论坛网站不同,我们不使用“谢谢”或“任何帮助表示赞赏”,或在[so]上签名。请参阅“[应该'嗨','谢谢',标语和致敬从帖子中删除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts)。 – 2014-10-28 01:17:03

+1

你的方法应该返回一个'List'或'Dictionary'而不是'out'参数 – Jonesopolis 2014-10-28 01:37:20

,而不是返回一个名字,我想你需要创建一个字典,并在循环中,把每个学生并在词典中评分。

从函数返回字典进行进一步处理。