决定和方法

Can*_* me 4 java if-statement

书中的问题:

编写一个程序,将字母等级翻译成数字等级.信的成绩A,B,C,D,和F,可能跟随+.它们的数值是4,3,2,1,和0.没有F+F–.A +增加数值0.3,减少数量0.3.但是,A+有价值4.0.

输入字母等级:B-数值为2.7.使用带有方法的等级getNumericGrade.

所以我的问题是:我知道我的代码是正确的,我可以运行它.但我很好奇是否有这个代码的快捷方式或更好的版本,使它看起来更专业,更容易阅读.任何建议或例子都将非常感激.

码:

import java.util.Scanner;


public class grade
{
    private double numericValue = 0;
    private String grade = "";

    public grade()
    {

        Scanner in = new Scanner(System. in );
        System.out.print("Enter Grade: ");
        grade = in .nextLine();
    }

    public double getNumericGrade()
    {

        if (grade.equals("A+") || grade.equals("A"))
        {
            numericValue = 4.0;
        }
        else if (grade.equals("A-"))
        {
            numericValue = 3.7;
        }
        else if (grade.equals("B+"))
        {
            numericValue = 3.3;
        }
        else if (grade.equals("B"))
        {
            numericValue = 3.0;
        }
        else if (grade.equals("B-"))
        {
            numericValue = 2.7;
        }
        else if (grade.equals("C+"))
        {
            numericValue = 2.3;
        }
        else if (grade.equals("C"))
        {
            numericValue = 2.0;
        }
        else if (grade.equals("C-"))
        {
            numericValue = 1.7;
        }
        else if (grade.equals("D+"))
        {
            numericValue = 1.3;
        }
        else if (grade.equals("D"))
        {
            numericValue = 1.0;
        }
        else if (grade.equals("F"))
        {
            numericValue = 0;
        }
        else
        {
            System.out.println("Letter not in grading system");
        }
        return numericValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

S.D*_*.D. 6

您可以单独定义映射和规则:

public static enum Grade {

    // Letter grades are A, B, C, D, and F
    // Their numeric values are 4, 3, 2, 1, and 0

    A(4),B(3),C(2),D(1),F(0);

    public final double score;

    private Grade(double d) {
        this.score = d;
    }

    // Grades are possibly followed by + or –
    // There is no F+ or F–
    // a + increases the numeric value by 0.3, a – decreases it by 0.3
    // However, an A+ has value 4.0

    public double getModifiedScore(char sign) {
        switch (sign) {
            case '+':
                return score + (score < 4 && score > 0 ? 0.3 : 0);
            case '-':
                return score + (score > 0 ? -0.3 : 0);
            default:
                throw new IllegalArgumentException("Invalid sign");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后只使用它们(例子假设您已经验证了输入):

public static double getNumericGrade(String s){
    Grade g = Grade.valueOf(s.substring(0, 1));
    if(s.length() > 1){
        return g.getModifiedScore(s.charAt(1));
    }else {
        return g.score;
    }
}
Run Code Online (Sandbox Code Playgroud)


Thi*_*ilo 5

我会使用查找表:

private final static Map<String,Double> gradeLookup = 
   new HashMap<String, Double>();

gradeLookup.put("A-", 3.7);

numericValue = gradeLookup.get(grade);
Run Code Online (Sandbox Code Playgroud)

另一个好的选择是switch语句,从Java 7开始,它最终适用于Strings.

这两个选项都为您提供内置输入验证,因此用户无法输入"G"或"F +"或"foo"等内容.

  • @arshaji:我只考虑如果+/-计算真正独立于等级查找.实际上,它是高度耦合的.我选择算法简单而不是难以维护的代码. (2认同)