为什么它总是返回原始价值?

时间:2012-06-29 05:26:01

标签: java android syntax casting

我有以下功能

private Integer calculateWeeklyValue(String p_Value, String Frequency) {

    if (p_Value.length() == 0)
        p_Value = "0";
    Integer Value = Integer.parseInt(p_Value);
    if (Frequency.equals("W"))
        return (Integer)Value;
    else if (Frequency.equals("F"))
        return (Integer)((Value / 2));
    else if (Frequency.equals("M"))
        return (Integer)((Value * 12) / 52);
    else if (Frequency.equals("Q"))
        return (Integer)((Value * 4) / 52);
    else if (Frequency.equals("Y"))
        return (Integer)(Value / 52);
    else
        return 0;

}

它总是返回变量“Value”的calue,即使它匹配不同的情况。 例如。它转到“return(整数)((Value * 12)/ 52);”但后来返回0并返回“值”。可能是一个愚蠢的问题,但坚持这一点。

3 个答案:

答案 0 :(得分:2)

使用int。并检查Value始终为0。

 int Value = Integer.parseInt(p_Value); and return type also `int`.

答案 1 :(得分:0)

看起来p_Value的长度始终为0.此外,使用大写首字母命名变量是不好的做法。

答案 2 :(得分:0)

您只需要将方法的返回类型从Integer更改为double或long或float 因为int总是返回数字而不是小数部分而你的计算返回.something并且转换为0所以确保使用这种计算方法的返回类型总是double或float等。

private Integer calculateWeeklyValue(String p_Value, String Frequency) {}

                     to 

private double calculateWeeklyValue(String p_Value, String Frequency) 
相关问题