从方法返回字符

时间:2019-01-22 13:38:32

标签: java

方法classify()中的条件仅返回字符'F'。如何使用if条件更改返回字符。例如,如果每周净工资是2500,则应返回“ C”。

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Name: ");
    String name=sc.nextLine();

    System.out.print("Enter Weekly Gross: ");
    double gross= sc.nextDouble();
    double gross1=netWeekly(gross);
    char classy=classify(gross1);
    double bonus = gross*0.085;
    System.out.println();
    System.out.println();
    System.out.println();

    System.out.println("*****SNOW ENTERPRISES*****");
    System.out.println("Employee Name: "+name);
    System.out.println("Gross Salary: "+gross);
    System.out.println("Bonus: "+bonus);
    System.out.println("Net Weekly Salary: "+gross1);
    System.out.println("Classification: "+classy);
}
public static double netWeekly(double a){
    double net=0;
    net = a/4.5; 
    return net;
}
public static char classify(double net){
    double crah=netWeekly(net);

    char letter='F';
    if(crah>1500&&crah<1999)
        letter = 'A';

    else if(crah>2000&&crah<2499)
        letter = 'B';
    else if(crah>2500&&crah<2999)
        letter = 'C';
    else if(crah>3000&&crah<3499)
        letter = 'D';
    else if(crah>3500&&crah<3999)
        letter = 'E';
    else if(crah>4000)
        return letter = 'F';

    return letter;
}

}

2 个答案:

答案 0 :(得分:4)

您的条件都没有与2500值完全匹配。

更改:

else if(crah>2500&&crah<2999)

else if(crah>=2500&&crah<3000)

(以及其他类似地方)


oleg.cherednik's建议使用Map可能可行,但我建议采用另一种实现方式:数字除法仅适用于bin大小相等且从对齐边界开始的情况;使其适应不同的边界将很尴尬。

一种替代方法是使用NavigableMap,该方法具有floorEntry方法来获取下一个最小的条目。例如,您可以使用TreeMap

private static final NavigableMap<Double, Character> MAP;

static {
    Map<Double, Character> map = new HashMap<>();
    // Handles everything less than 1500.
    map.put(Double.NEGATIVE_INFINITY, 'F');
    map.put(1500.0, 'A');
    map.put(2000.0, 'B');
    map.put(2500.0, 'C');
    map.put(3000.0, 'D');
    map.put(3500.0, 'E');
    map.put(4000.0, 'F');
    MAP = Collections.unmodifiableNavigableMap(map);
}

然后:

public static char classify(double net) {
    return MAP.floorEntry(netWeekly(net)).getValue();
}

答案 1 :(得分:0)

您忘记了>=比较。怎样简化这个逻辑呢?

public static char classify(double net) {
    double crah = netWeekly(net);

    if (crah >= 4000)
        return 'F';
    if (crah >= 3500)
        return 'E';
    if (crah >= 3000)
        return 'D';
    if (crah >= 2500)
        return 'C';
    if (crah >= 2000)
        return 'B';
    if (crah >= 1500)
        return 'A';

    return 'F';
}

甚至是这样:

public static char classify(double net) {
    return MAP.get((int)netWeekly(net) / 500 * 500);
}

private static final Map<Integer, Character> MAP;

static {
    Map<Integer, Character> map = new HashMap<>();
    map.put(0, 'F');
    map.put(1500, 'A');
    map.put(2000, 'B');
    map.put(2500, 'C');
    map.put(3000, 'D');
    map.put(3500, 'E');
    map.put(4000, 'F');
    MAP = Collections.unmodifiableMap(map);
}