使用带有枚举的switch语句

时间:2015-02-15 08:00:31

标签: java enums

我在此网站的表单中找到了以下内容。我添加了一些代码来获取用户输入。但是在switch语句的开头有一个名为NullPointerExecption的异常。

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double gpa;
        System.out.println("Enter your GPA: ");
        gpa = input.nextDouble();
        Grade grade = Grade.getGradeFromGPA(gpa);



        switch (grade) {
            case FIRST_CLASS:   System.out.println("First class Hons"); break;
            case TWO_ONE:       System.out.println("upper Second class Hons"); break;
            case TWO_TWO:       System.out.println("Lower Second class Hons"); break;
            case PASS:          System.out.println("Pass"); break;
            case FAIL:          System.out.println("Fail"); break;
            default:            System.out.println("Invalied GPA");
        }
    }

    private enum Grade {
        FIRST_CLASS(3.5, 4),
        TWO_ONE(3, 3.49),
        TWO_TWO(2.5, 2.99),
        PASS(2.49, 2),
        FAIL(0, 1.99);

        private final double minGPA;
        private final double maxGPA;

        Grade(final double minGPA, final double maxGPA) {
            this.minGPA = minGPA;
            this.maxGPA = maxGPA;
        }

        static Grade getGradeFromGPA(double gpa) {
            for (Grade grade : Grade.values()) {
                if (grade.maxGPA >= gpa && grade.minGPA <= gpa) {
                    return grade;
                }
            }
            //invalid GPA
            return null;
        }
    }

我认为它不会通过getGradeFromGPA()方法返回任何内容。

2 个答案:

答案 0 :(得分:1)

如果您输入无效的GPA,Grade.getGradeFromGPA(gpa)将返回null,并且无法在switch声明中使用。

如果您愿意,可以明确处理:

Grade grade = Grade.getGradeFromGPA(gpa);

if (grade == null) {
    System.out.println("Invalid GPA");
}
else {
    switch (grade) {
        case FIRST_CLASS:   System.out.println("First class Hons"); break;
        case TWO_ONE:       System.out.println("upper Second class Hons"); break;
        case TWO_TWO:       System.out.println("Lower Second class Hons"); break;
        case PASS:          System.out.println("Pass"); break;
        case FAIL:          System.out.println("Fail"); break;
    }
}

答案 1 :(得分:0)

  

我认为它不会通过getGradeFromGPA()方法返回任何内容。

这是对的。如果您输入例如55getGradeFromGPA将返回null

请注意,有些双打无法准确表示,因此如果您输入2.49,这也会产生null

(另外,您似乎已经在PASS枚号上交换了参数。)

  

如何解决这个问题?

关于double的问题,我建议您这样做:

private enum Grade {
    FIRST_CLASS(3.5),
    TWO_ONE(3),
    TWO_TWO(2.5),
    PASS(2),
    FAIL(0);

    private final double minGPA;

    Grade(final double minGPA) {
        this.minGPA = minGPA;
    }

    static Grade getGradeFromGPA(double gpa) {
        for (Grade grade : Grade.values()) {
            if (gpa > grade.minGPA)
                return grade;
        }
        //invalid GPA
        return null;
    }
}

然而,当您输入55时需要处理的情况。最好的方法可能是使用例外:

static Grade getGradeFromGPA(double gpa) {
    for (Grade grade : Grade.values()) {
        if (gpa > grade.minGPA)
            return grade;
    }
    //invalid GPA
    throw new IllegalArgumentException();  // <----------
}

try {
    Grade grade = Grade.getGradeFromGPA(gpa);
    switch (grade) {
        case FIRST_CLASS:   System.out.println("First class Hons"); break;
        case TWO_ONE:       System.out.println("upper Second class Hons"); break;
        case TWO_TWO:       System.out.println("Lower Second class Hons"); break;
        case PASS:          System.out.println("Pass"); break;
        case FAIL:          System.out.println("Fail"); break;
    }
} catch (IllegalArgumentException e) {
    System.out.println("Invalied GPA");
}