-4

我正在制作一个计算一个人的gpa的程序。我的变量包括A+等...

任何帮助将不胜感激,谢谢

4

2 回答 2

2

我会创建你自己的枚举类。

enum Grade {
    A(4.0, "A"), B_PLUS(3.5, "B+"), B(3.0, "B"),  // etc.
      F(0, "F");

    private float gradePoints;
    private String asString;

    private Grade(float gradePoints, String asString) {
        this.gradePoints = gradePoints;
        this.asString = asString;
    }

    public float getGradePoints() {
        return gradePoints;
    }

    @Override
    public String toString() {
        return asString;
    }
}
于 2013-02-28T20:59:00.817 回答
0

这可能是领域驱动设计中价值对象的一个​​很好的候选者。

public class GpaGrade {
    private Int32 _inPercent;

    public GpaGrade(Int32 inPercent) {
        // TODO: check that inPercent is between 1 and 100
        _inPercent = inPercent;
    }

    public Int32 AsPercents() {
        return _inPercent;
    }

    public Double In40Scale() {
        // TODO: convert to 4.0 scale
    }

    public String AsLetterCode() {
        // TODO: convert to letter code
    }

    // TODO: override Equals and GetHashCode
}
于 2013-02-28T20:58:49.137 回答