在java中使用枚举来制作一个等级标尺,然后允许用户输入数字等级来输出字母等级

wnvonmuf  于 2021-07-12  发布在  Java
关注(0)|答案(5)|浏览(350)

我正在进行第四周的java在线学习,我有一个作业要用枚举法来创建一个评分量表。
我已经创建了分级规模枚举,但完全不知道如何让用户输入一个数字值,然后输出字母值。每次尝试,它要么告诉我不能对所选内容使用返回函数,要么system.out.println也不想接受我的值。我决定完全废弃我的类文件并重新启动,但不知道我做错了什么。这是我的枚举码:

public enum Grade {

    A(95-100, "A"),
    AMINUS(92-94, "A-"),
    BPLUS(89-91, "B+"),
    B(86-88, "B"),
    BMINUS(83-85, "B-"),
    CPLUS(80-82, "C+"),
    C(77-79, "C"),
    CMINUS(74-76, "C-"),
    DPLUS(71-73, "D+"),
    D(68-70, "D"),
    DMINUS(65-67, "D-"),
    F(1-64, "F"),
    FA(0, "FAILURE TO APPEAR");

    private final String gradeText; 

    private Grade(int gradeValue, String gradeText) {
        this.gradeText = gradeText; 
    }

    public String printGrade() {
        return gradeText; 
    }
}
oyt4ldly

oyt4ldly1#

我认为这种方法可以帮助你。
与其尝试在枚举中存储范围,不如为每个级别存储仍可视为该级别的最大值。然后,您可以借助静态方法检索实际的字母等级:

public enum Grade {

    A(100, "A"), AMINUS(94, "A-"), BPLUS(91, "B+"), B(88, "B"), BMINUS(85, "B-"),
    CPLUS(82, "C+"), C(79, "C"), CMINUS(76, "C-"), DPLUS(73, "D+"), D(70, "D"),
    DMINUS(67, "D-"), F(64, "F"), FA(0, "FAILURE TO APPEAR");

    private final int gradeValue;
    private final String gradeText;

    private Grade(int gradeValue, String gradeText) {
        this.gradeValue = gradeValue;
        this.gradeText = gradeText;
    }

    public static Grade getGrade(int value) {
        Grade grade = FA;
        for(Grade g : values()) {
            if(value <= g.gradeValue) {
                grade = g;
            }
        }
        return grade;
    }

    public String printGrade() {
        return gradeText;
    }
}

测试:

System.out.println(Grade.getGrade(99).printGrade());
System.out.println(Grade.getGrade(80).printGrade());
System.out.println(Grade.getGrade(88).printGrade());
System.out.println(Grade.getGrade(75).printGrade());

告诉我们:

A
C+
B
C-
w6lpcovy

w6lpcovy2#

您需要将边界定义为单独的字段,以便编译器理解。可能您正在查找这样的枚举:

public enum Grade {
    A("A", 95, 100),
    AMINUS("A-", 92, 94);
    // DEFINE ALL YOUR GRADES HERE

    String grade;
    int gradeMin;
    int gradeMax;

    Grade(String grade, int gradeMin, int gradeMax) {
        this.grade = grade;
        this.gradeMin = gradeMin;
        this.gradeMax = gradeMax;
    }

    /**
     * @return the grade
     */
    public String getGrade() {
        return grade;
    }

    /**
     * @return the gradeMin
     */
    public int getGradeMin() {
        return gradeMin;
    }

    /**
     * @return the gradeMax
     */
    public int getGradeMax() {
        return gradeMax;
    }

    public static Grade getGrade(int marks) {
        for (Grade g : values()) {
            if (marks >= g.getGradeMin() && marks <= g.getGradeMax()) {
                return g;
            }
        }

        return null;
    }
}

然后你可以在你的课堂上用这个来获得分数

public class GradeRetriever {
    public static void main(String... args) {
        System.out.println(Grade.getGrade(92).getGrade());
    }
}
woobm2wo

woobm2wo3#

我猜你是想达到这样的目标:

public enum Grade {

    A(95, 100, "A"),
    AMINUS(92, 94, "A-"),
    BPLUS(89, 91, "B+"),
    B(86, 88, "B"),
    BMINUS(83, 85, "B-"),
    CPLUS(80, 82, "C+"),
    C(77, 79, "C"),
    CMINUS(74, 76, "C-"),
    DPLUS(71, 73, "D+"),
    D(68, 70, "D"),
    DMINUS(65, 67, "D-"),
    F(1, 64, "F"),
    FA(0, 0, "FAILURE TO APPEAR");

    private final String gradeText;
    private final int lower;
    private final int higher;

    private Grade(int lower, int higher, String gradeText) {
        this.gradeText = gradeText;
        this.lower = lower;
        this.higher = higher;
    }

    public String getGradeText() {
        return this.gradeText;
    }

    public static Grade getGrade(int points) {
        for (Grade g : Grade.values()) {
            if (g.lower <= points && points <= g.higher) {
                return g;
            }
        }

        return null;
    }

    public static void main(String[] args) {
        for (int i = 0; i <= 100; i++) {
            System.out.println(Grade.getGrade(i));
        }
    }
}

我用两个单独的参数替换了“range”定义,将它们存储到enum字段中,并添加了一个 getGrade(int points) 方法,返回 Grade 与给定点数对应的枚举项。如果输入的数字不在 0100 ,方法返回null。这个 main 方法只是完成这个简单的概念证明和一个如何使用的例子 getGrade(int) 方法。

3lxsmp7m

3lxsmp7m4#

构造器不会像你使用它那样工作。如果要向枚举类型中添加值(如本文所述),则需要在构造函数中设置这些值。
因此,枚举类型只需要一个构造函数,而另一个java类永远不能使用该构造函数。
由于java中没有easy range类,我建议您将最小值和最大值定义为单独的参数:

public enum Grade {
    A(95, 100, "A")
    // and so on

    String gradeText;
    int gradeMinPoints;
    int gradeMaxPoints;

    // ...
}

在构造函数中,需要指定以下值:

private Grade (int minimum, int maximum, String text) {
    this.gradeMinPoints = minimum;
    this.gradeMaxPoints = maximum;
    this.gradeText      = text;
}

您可以通过其他函数访问变量,并根据您提供的参数声明返回“grade”枚举类型的静态函数。这篇文章的其他答案提供了比我所能写的更好的功能,所以我会让你参考这些。

zaq34kh6

zaq34kh65#

public enum Grade {
    A("A", 95, 100),
    AMINUS("A-", 92, 94),
    BPLUS("B+", 89, 91),
    B("B", 86, 88),
    BMINUS("B-", 83, 85),
    CPLUS("C+", 80, 82),
    C("C", 77, 79),
    CMINUS("C-", 74, 76),
    DPLUS("D+", 71, 73),
    D("D", 68, 70),
    DMINUS("D-", 65, 67),
    F("F", 1, 64),
    FA("FAILURE TO APPEAR", 0, 0);

    private final String text;
    private final int min;
    private final int max;

    Grade(String text, int min, int maxScore) {
        this.text = text;
        this.min = min;
        this.max = maxScore;
    }

    public String getText() {
        return text;
    }

    public static Grade parseScore(int score) {
        for (Grade grade : values())
            if (score >= grade.min && score <= grade.max)
                return grade;
        throw new EnumConstantNotPresentException(Grade.class, String.valueOf(score));
    }
}

现在你可以把分数转换成 Gradle :

Grade grade = Grade.parseScore(85);  // BMINUS

相关问题