亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Java中的枚舉制作評分等級,然后允許用戶輸入數字等級以輸出字母等級

使用Java中的枚舉制作評分等級,然后允許用戶輸入數字等級以輸出字母等級

精慕HU 2022-06-04 17:18:59
我正在在線學習 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;     }}
查看完整描述

3 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

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


查看完整回答
反對 回復 2022-06-04
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

您需要將邊界定義為單獨的字段,以便編譯器理解??赡苣趯ふ疫@樣的枚舉:


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());

    }

}


查看完整回答
反對 回復 2022-06-04
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

構造函數不會按照您使用它的方式工作。如果您想將值添加到枚舉類型,就像您在此處所做的那樣,您需要在構造函數中設置這些值。


因此,枚舉類型只需要一個構造函數,并且構造函數永遠不能被另一個 java 類使用。


由于 Java 中沒有簡單的范圍類,我建議您將最小值和最大值定義為單獨的參數:


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;

}

您可以通過其他函數訪問變量,并根據您提供的參數聲明返回“等級”枚舉類型的靜態函數。這篇文章的其他答案提供的功能比我寫的要好得多,所以我會向你推薦那些。


查看完整回答
反對 回復 2022-06-04
  • 3 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號