1

在这段代码中,我认为我几乎是正确的,但它并没有按照我需要的方式计算。代码应该忽略第一个等级,只有当第二个等级更高时。

这是我为它构建的代码,但是当我运行程序时,它总是会计算二年级而不管它的价值。

double ComputeGPA()
    {
        if (Count == 0) return 0;
        bool bForgiven = false;
        int nCourseCount = 0;
        int i;
        double gpa = 0.0;
        double gpaToAdd;
        for (i = 0; i < this.Count; i++)
        {
            gpaToAdd = 0.0; 
            Course c = this[i]; 
            gpaToAdd = GradePoints(c.Grade);
            if (c.Grade == null || c.Grade == "W") continue;
            if (bForgiven == false)
            {
                int nRep = FindCourse(c.Number, i + 1);
                if (nRep > 0)
                {
                    Course x = this[i + 1];
                    if(GradePoints(this[nRep].Grade > GradePoints(c.Grade)))
                        gpaToAdd = GradePoints(x.Grade); 
                    // This means we forgive only one grade
                    bForgiven = true;
                    continue;
                }
            }
        gpa = gpa + GradePoints(c.Grade);
        nCourseCount++;
    }
    //If we've forgiven a grade , we divide by one less course
    gpa = (nCourseCount > 0) ? gpa / nCourseCount : 0.0;
    return gpa;
}

我也有这些功能可以使用它:

public int FindCourse(int Number, int nStart)
       {
           int i;
           for (i = nStart; i< this.Count; i++)
           {
               Course c = this[i];
               if (c.Number == Number) return i;
           }
           return -1; // Signifies no course was found
       }

public int FindCourse(int Number)
{
    return FindCourse(Number, 0);
}

什么结构可以让它发挥作用?

谢谢你,特拉维斯

以下是一些测试输入:

TestTranscript();
    }
    static void TestTranscript()
    {
        Transcript trans = new Transcript();
        trans.Add(new Course(1, 3113, "A", false));
        trans.Add(new Course(1, 3232, "A", false));
        trans.Add(new Course(1, 4212, "A", false));
        trans.Add(new Course(1, 3113, "F", false));
        trans.Add(new Course(1, 4220, "A", false));
        trans.Add(new Course(1, 4234, "A", false));
        trans.Add(new Course(1, 4300, "A", false));
        TranscriptForm frm = new TranscriptForm("Test Transcript", trans);
        frm.ShowDialog();
        MessageBox.Show("GPA is computed to be " + trans.GPA.ToString());
    }

这些是每个字母对应的值:

 public static double GradePoints(string grade)
    {
        switch (grade)
        {
            case "A":
            case "A+":
                return 4.0;
            case "A-":
                return 3.7;
            case "B+":
                return 3.3;
            case "B":
                return 3.0;
            case "B-":
                return 3.7;
            case "C+":
                return 2.3;
            case "C":
                return 2.0;
            case "C-":
                return 1.7;
            case "D+":
                return 1.3;
            case "D":
                return 1.0;
            case "D-":
            default:
                return 0.0;
        }
    }
4

3 回答 3

1

除了 Chris Shain 的评论之外,这部分对我来说看起来很奇怪:

 Course x = this[i + 1];
 if(GradePoints(this[nRep].Grade > GradePoints(c.Grade)))
   gpaToAdd = GradePoints(x.Grade); 

为什么在这里 i+1 - 这不是列表中的下一个,不一定是相同的课程编号?gpaToAdd 不应该是更好的成绩(this[nRep].Grade)吗?

于 2012-07-06T17:43:15.727 回答
0

有两个问题导致错误的计算发生。

1)正如其他人所提到的, continue 语句是一个问题。这里的问题是,当您跳过该项目时,您只希望它继续(并设置 bForgiven)。换句话说,它需要包含在 if 语句中,如下所示:

if (GradePoints(trans[nRep].Grade) > GradePoints(c.Grade))
{
    gpaToAdd = GradePoints(x.Grade);
    // This means we forgive only one grade
    bForgiven = true;
    continue;
}

2)你正在用这条线循环“这个”

for (i = 0; i < this.Count; i++)

稍后您将使用此行搜索相同“c.Number”的另一个成绩

int nRep = FindCourse(c.Number, i + 1);

问题是您正在寻找后面的(在列表中)数字并且从不与早期的记录进行比较,因此当它到达第一个“3113”(记录 0)时,它会找到并与第二个(记录 3)进行比较,但是当它到达记录 3 时,它永远不会找到第一个,因为它是从记录 4 开始搜索的。

解决此问题的方法是 FindCourse 从头开始​​搜索到当前,因此在您的示例中,它会触发记录 3 而不是像这样的记录 0 的命中。

public int FindCourse(int Number, int nCurrent)
{
    int i;
    for (i = 0; i < nCurrent; i++)
    {
        Course c = this[i];
        if (c.Number == Number) return i;
    }
    return -1; // Signifies no course was found
}

注意:这将需要像这样使用 i 而不是 i+1 调用。

int nRep = FindCourse(c.Number, i);
于 2012-07-06T17:47:25.047 回答
0

看起来像一个流量控制问题。请参阅我在声明中的continue说明:

for (i = 0; i < this.Count; i++)
    {
        gpaToAdd = 0.0; 
        Course c = this[i]; 
        gpaToAdd = GradePoints(c.Grade);
        if (c.Grade == null || c.Grade == "W") continue;
        if (bForgiven == false)
        {
            int nRep = FindCourse(c.Number, i + 1);
            if (nRep > 0)
            {
                Course x = this[i + 1];
                if(GradePoints(this[nRep].Grade > GradePoints(c.Grade)))
                    gpaToAdd = GradePoints(x.Grade); 
                // This means we forgive only one grade
                bForgiven = true;

                // This will restart the for loop at the next item, 
                // presumably skipping whatever code you left out
                // below
                continue; 
            }
        }

    // Something else is going on down here that you didnt show us-
    // my guess is something like:
    gpa = gpaToAdd + gpa;
    // but it is getting skipped when a grade is forgiven

此处记录了继续:http: //msdn.microsoft.com/en-us/library/923ahwt1 (v=vs.80).aspx

于 2012-07-06T17:34:56.957 回答