Java Grade类,字母等级到数字

时间:2013-05-24 21:51:08

标签: java

我正在写一个字母等级的程序。 A为4.0,B为3.0,依此类推。如果你输入B +或B-,它将分别减去.3或增加.3(所以B +将是3.3)。

在我的代码中,当我测试像B +这样的字母时,它给了我.3而不是实际返回实际等级的减去值。我做错了什么?

public class Grade {
    private String grade;
    private double gradeNum;

    // Constructor
    public Grade(String showGrade) {
        grade = showGrade;
        gradeNum = 0;
    }

    // getNumericGrade method to return grade number
    public double getNumericGrade() {
        String suffix;
        suffix = grade.substring(1);

        if(suffix.equals("+")) {
            gradeNum = gradeNum + .3;
        } else if(suffix.equals("-")) {
            gradeNum = gradeNum - .3;
        }

        String letterGrade = grade.substring(0);

        if(letterGrade.equalsIgnoreCase("A")) {
            gradeNum = 4.0;
        } else if(letterGrade.equalsIgnoreCase("B")) {
            gradeNum = 3.0;
        } else if(letterGrade.equalsIgnoreCase("C")) {
            gradeNum = 2.0;
        } else if(letterGrade.equalsIgnoreCase("D")) {
            gradeNum = 1.0;
        } else if(letterGrade.equalsIgnoreCase("F")) {
            gradeNum = 0.0;
        } else {
            System.out.println("Invalid letter grade");
        }

        return gradeNum;
    }
}

5 个答案:

答案 0 :(得分:3)

您应首先处理您的字母成绩,以便正确初始化gradeNum。另请注意,substring(int index)返回从索引开始直到字符串结尾的子序列。因此,没有任何letterGrade if块匹配A +,B-等输入

if (grade == null || !grade.matches("[A-F][+-]?")) // if grade wasn't specified, or
{// if grade is not between A to F *optionally* followed by a + or - sign
    System.out.println("Invalid letter grade");
    return gradeNum;
}

String letterGrade = grade.substring(0, 1); // pass 1 to exclude + or -

if (letterGrade.equalsIgnoreCase("A"))
{
    gradeNum = 4.0;
}
else if(letterGrade.equalsIgnoreCase("B"))
{
    gradeNum = 3.0;
}
else if(letterGrade.equalsIgnoreCase("C"))
{
  gradeNum = 2.0;
}
else if(letterGrade.equalsIgnoreCase("D"))
{
    gradeNum = 1.0;
}
else if(letterGrade.equalsIgnoreCase("F"))
{
    gradeNum = 0.0; // return from here if you want F = F- = F+
    return gradeNum; // if you want to invalidate F- and F+ look below
}
/* else // not required; already validated by using regex above
{
    System.out.println("Invalid letter grade");
    return gradeNum; // don't forget to RETURN from here
}*/

然后进行后缀处理,添加或减去gradeNum现已正确初始化的内容。

String suffix;
suffix = grade.substring(1);
if (suffix.isEmpty()) { // return early if there's no suffix
    return gradeNum;
} else if (gradeNum == 0.0) { // to invalidate F- and F+
    System.out.println("Invalid letter grade");
    return gradeNum;
}
if(suffix.equals("+") && gradeNum != 4.0) // don't increment if grade is A+ (= A = 4.0)
{
    gradeNum = gradeNum + .3;
}
else if (suffix.equals("-"))
{
    gradeNum = gradeNum - .3;
}
return gradeNum;

答案 1 :(得分:1)

我根本不喜欢这种设计。

为什么要一次又一次地支付重新计算数值的罚款?这毫无意义。

使等级不可变,并在获得字母值时计算构造函数中的数值。

我还有另一个构造函数,可以使用float / double并设置字母等级。

您也可以考虑将Grade设为enum,如下所示:

/**
 * Grade encapsulates symbols with values
 * @author Michael
 * @link http://stackoverflow.com/questions/16744116/java-grade-class-letter-grade-to-number/16744210#16744210
 * @since 5/24/13 6:01 PM
 */
public enum Grade {
    APLUS("A+", 4.3),
    A("A", 4.0),
    AMINUS("A-", 3.7),
    BPLUS("B+", 3.3),
    B("B", 3.0),
    BMINUS("B-", 2.7),
    CPLUS("C+", 2.3),
    C("C", 2.0),
    CMINUS("C-", 1.7),
    DPLUS("D+", 1.3),
    D("D", 1.0),
    DMINUS("D-", 0.7),
    F("F", 0.0);

    private final String symbol;
    private final double value;

    Grade(String symbol, double value) {
        this.symbol = symbol;
        this.value = value;
    }

    public String getSymbol() {
        return symbol;
    }

    public double getValue() {
        return value;
    }
}

答案 2 :(得分:1)

收到你的来信后,你可以使用

String letterGrade = grade.substring(0);

但你永远不会指定结束位置。你可以做以下

String letterGrade = grade.substring(0,1);  // the end position is exclusive

here's some substring documentation


是的,您应该在设置字母值后添加/减去+-修改

答案 3 :(得分:0)

您需要重新订购代码:

private String grade;
private double gradeNum;

// Constructor
public Grade(String showGrade)
{
  grade = showGrade;
  gradeNum = 0;
}


// getNumericGrade method to return grade number
public double getNumericGrade()
{
    String letterGrade = grade.substring(0);

    if (letterGrade.equalsIgnoreCase("A"))
    {
        gradeNum = 4.0;
    }

    else if(letterGrade.equalsIgnoreCase("B"))
    {
        gradeNum = 3.0;

    }

    else if(letterGrade.equalsIgnoreCase("C"))
    {
      gradeNum = 2.0;
    }

    else if(letterGrade.equalsIgnoreCase("D"))
    {
        gradeNum = 1.0;
    }

    else if(letterGrade.equalsIgnoreCase("F"))
    {
        gradeNum = 0.0;
    }


    else
    {
        System.out.println("Invalid letter grade");
        return -1.0;
    }



    String suffix;
    suffix = grade.substring(1);

    if(suffix.equals("+"))
    {
        gradeNum = gradeNum + .3;

    }

    else if (suffix.equals("-"))
    {
        gradeNum = gradeNum - .3;
    }







    return gradeNum;

}

答案 4 :(得分:-1)

编写一个java程序,要求用户输入字母等级编号,程序将根据下表显示对应的字母等级和输入字母编号的点