问题描述:

I have created 2 classes one that is the enumeration class to determine what the grades someone received are and the other is supposed to be a simple program to call it and display the gpa value and the pass or fail value

here is the enumeration class

package root;

public enum LetterGrade

{

a ("A", 4.0, true), aMinus ("A-", 3.7, true),

bPlus ("B+", 3.3, true), b ("B", 3.0, true), bMinus ("B-", 2.7, true),

cPlus ("C+", 2.3, true), c ("C", 2.0, true), cMinus ("C-", 1.7, true),

dPlus ("D+", 1.3, false), d ("D", 1.0, false), dMinus ("D-", 0.7, false),

f ("F", 0.0, false);

private final String grade;

private final double points;

private final boolean pass;

private LetterGrade (String letterGrade, double gradePoints, boolean isPass)

{

grade = letterGrade;

points = gradePoints;

pass = isPass;

}

public String getletterGrade ()

{

return grade;

}

public boolean isPass()

{

return pass;

}

public double getgradePoints ()

{

return points;

}

}

here is the other class

package root;

import root.LetterGrade;

import java.util.Scanner;

public class Ch6PP5_RobertSaunders {

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

String grade;

System.out.println("Please input the grade you recieved.");

grade = keyboard.next();

LetterGrade = new LetterGrade(String grade);

System.out.println("");

}

}

I am stuck on how to call the enum values, my test inputs at the moment are A, b, A+, aPlus, f, and e but i have no idea how to set them up as an input for a test yet

网友答案:

I would add a method to get the LetterGrade from a double to the enum. Something like,

public static LetterGrade fromDouble(double grade) {
    int len = values().length;
    for (int i = 0; i < len; i++) {
        if (grade >= values()[i].getgradePoints())
            return values()[i];
    }
    return LetterGrade.f;
}

And I suggest you override toString() like

@Override
public String toString() {
    return grade;
}

Then your main() method can use it like,

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please input the grade you recieved.");
    double grade = keyboard.nextDouble();
    LetterGrade lg = LetterGrade.fromDouble(grade);
    System.out.println(lg);
}
网友答案:

Think about adding a (static) method to LetterGrade that retrieves the correct instance based on the user input:

LetterGrade usersGrade = LetterGrade.retrieveFromInput(grade);

You probably need to utilize a few of the helper methods that enum provides...

相关阅读:
Top