你可以在if语句之后做一个if语句吗?

时间:2019-02-09 18:09:58

标签: java html if-statement

我的BMR计算器不能给我正确的结果,我知道是否可以检查活动的if循环是正确的答案,但是我找不到需要更改的地方

double doubleweight = Double.parseDouble(Weight);
double doubleage = Double.parseDouble(Age);
double doubleheight = Double.parseDouble(Height);

double BMRm, BMRf;
BMRm = 66 + (6.23 * doubleweight) + (12.7 * doubleheight) - (6.8 * doubleage);
BMRf = 655 + (4.35 * doubleweight) + (4.7 * doubleheight) - (4.7 * doubleage);

double bmr;
if ((null != Gender) && "Female".equals(Gender)){
bmr=BMRf;
}
else{
    bmr=BMRm;
}

if ("sed".equals(Activity)){
    bmr=(bmr*1.2);
}
else if("lightact".equals(Activity)){
    bmr=(bmr*1.375);
}
else if("modact".equals(Activity)){
    bmr=(bmr*1.55);
}
else if("vact".equals(Activity)){
    bmr=(bmr*1.725);
}
else{
    bmr=(bmr*1.9);
}

1 个答案:

答案 0 :(得分:0)

您最好使用 case 语句,因为它更简单,在尝试编写计算器代码时特别有用:

switch  (Activity) {
    case "sed" : bmr=(bmr*1.2);
    case "lightact" : bmr=(bmr*1.375);
    case "modact" : bmr=(bmr*1.55);
    case "vact" : bmr=(bmr*1.725);
    default: bmr=(bmr*1.9);
}

为获得最佳实践,属性名称最好以小写字母开头:

double bmrm = 66 + (6.23 * doubleweight) + (12.7 * doubleheight) - (6.8 * doubleage);
double bmrf = 655 + (4.35 * doubleweight) + (4.7 * doubleheight) - (4.7 * doubleage);