Java 误差方法过于复杂,无法用数据流算法进行分析

Java 误差方法过于复杂,无法用数据流算法进行分析,java,android,css,Java,Android,Css,这就是导致问题的方法。我正在创建一个BMI计算器,它使用年龄、体重和身高来计算最终结果。 我不确定我的逻辑是否错误,或者是否还有其他问题 public void calculateClickHandler(View view) { String Outcome; Outcome = null; age = Float.parseFloat(txtHowOld.getText().toString()); feet = Float.parseFloat(tx

这就是导致问题的方法。我正在创建一个BMI计算器,它使用年龄、体重和身高来计算最终结果。 我不确定我的逻辑是否错误,或者是否还有其他问题

public void calculateClickHandler(View view) {   
    String Outcome;
    Outcome = null;

    age = Float.parseFloat(txtHowOld.getText().toString());
    feet = Float.parseFloat(txtFt.getText().toString());
    inches = Float.parseFloat(txtIn.getText().toString());
    pounds = Float.parseFloat(txtWeight.getText().toString());
    height = (feet * 12) + inches;
    double BMI1 = (pounds / (height * height)) * 703.0;


    if (btnF.isChecked()) {
        if (age >= 20 && age <= 40) {
            if (BMI1 < 21) {
                Outcome = "Underweight";
            } else if (BMI1 >= 21 && BMI1 <= 33) {
                Outcome = "Healthy";
            }
            else if (BMI1 > 33 && BMI1 <= 39) {
                Outcome = "Overweight";
            } else if (BMI1 > 39) {
                Outcome = "Obese";
            } else if (age >= 41 && age <= 60) {
                if (BMI1 < 23) {
                    Outcome = "Underweight";
                } else if (BMI1 >= 23 && BMI1 <= 35) {
                    Outcome = "Healthy";
                } else if (BMI1 > 35 && BMI1 <= 40) {
                    Outcome = "Overweight";
                } else if (BMI1 > 40) {
                    Outcome = "Obese";
                }
            } else if (age >= 61 && age <= 79) {
                if (BMI1 < 24) {
                    Outcome = "Underweight";
                } else if (BMI1 >= 24 && BMI1 <= 36) {
                    Outcome = "Healthy";
                } else if (BMI1 > 36 && BMI1 <= 42) {
                    Outcome = "Overweight";
                } else if (BMI1 > 42) {
                    Outcome = "Obese";
                }

            }
        }

        if (btnM.isChecked()) {
            if (age >= 20 && age <= 40) {
                if (BMI1  < 8) {
                    Outcome = "Underweight";
                } else if (BMI1  >= 8 && BMI1  <= 19) {
                    Outcome = "Healthy";
                } else if (BMI1  > 19 && BMI1  <= 25) {
                    Outcome = "Overweight";
                } else if (BMI1  > 25) {
                    Outcome = "Obese";
                }
            } else if (age >= 41 && age <= 60) {
                if (BMI1  < 11) {
                    Outcome = "Underweight";
                } else if (BMI1  >= 11 && BMI1  <= 22) {
                    Outcome = "Healthy";
                } else if (BMI1  > 22 && BMI1  <= 27) {
                    Outcome = "Overweight";
                } else if (BMI1  > 27) {
                    Outcome = "Obese";
                }
            } else if (age >= 61 && age <= 79) {
                if (BMI1  < 13) {
                    Outcome = "Underweight";
                } else if (BMI1  >= 14 && BMI1  <= 25) {
                    Outcome = "Healthy";
                } else if (BMI1  > 25 && BMI1  <= 27) {
                    Outcome = "Overweight";
                } else if (BMI1  > 27) {
                    Outcome = "Obese";
                }
            }


            BMI2.setText(Outcome);
        }
    }
}

}
public void calculateClickHandler(视图){
字符串结果;
结果=null;
age=Float.parseFloat(txtHowOld.getText().toString());
feet=Float.parseFloat(txtFt.getText().toString());
英寸=Float.parseFloat(txtIn.getText().toString());
磅=Float.parseFloat(txtwweight.getText().toString());
高度=(英尺*12)+英寸;
双倍BMI1=(磅/(身高*身高))*703.0;
if(btnF.isChecked()){
如果(年龄>=20&&age=21&&BMI1 33&&BMI1 39){
结果=“肥胖”;
}否则如果(年龄>=41岁&&age=23岁&&BMI1 35岁&&BMI1 40岁){
结果=“肥胖”;
}
}否则,如果(年龄>=61&&age=24&&BMI1 36&&BMI1 42){
结果=“肥胖”;
}
}
}
如果(btnM.isChecked()){
如果(年龄>=20&&age=8&&BMI1 19&&BMI1 25){
结果=“肥胖”;
}
}否则如果(年龄>=41岁&&age=11岁&&BMI1 22岁&&BMI1 27岁){
结果=“肥胖”;
}
}否则如果(年龄>=61岁&&age=14岁&&BMI1 25岁&&BMI1 27岁){
结果=“肥胖”;
}
}
BMI2.setText(结果);
}
}
}
}

您的逻辑很好。androidstudio(或其他什么?)只是抱怨有太多的分支来计算方法的数值复杂性

如果您想减轻错误,请创建一个新类,将BMI值(可能还有一个布尔值或性别枚举(我想这就是btnM的意思))解析到一个单独的类或方法中

---编辑

您可以在下面的代码中看到,通过首先找到适当的边界,然后将边界和BMI1计算为结果,消除了冗余范围检查代码

在制作这些的过程中,你也会注意到你的祖父范围(13/14)中有一个bug,并且对于19岁或80岁以上的人没有计算

public void calculateClickHandler(View view) {
    boolean male = btnM.isChecked() && !btnF.isChecked();
    age, feet, inches, pounds, BMI1 = whatever;
    String outcome = findOutcome(male, BMI1);
    // display outcome somewhere
}

static final int[] bmiRangesLady = { 21, 33, 39 };
static final int[] bmiRangesMom = { 23, 35, 40 };
static final int[] bmiRangesGma = { 24, 36, 42 };

static final int[] bmiRangesMan = { 8, 19, 25 };
static final int[] bmiRangesDad = { 11, 22, 27 };
static final int[] bmiRangesGpa = { 13, 25, 30 };

public static String findOutcome(boolean male, double bmi) {

    int[] bmiRangesToUse;

    if (!male) {
        if (age >= 20 && age <= 40) {
            bmiRangesToUse = bmiRangesLady;
        } else if (age > 40 && age <= 60) {
            bmiRangesToUse = bmiRangesMom;
        } else if (age > 60) {
            bmiRangesToUse = bmiRangesGma;
        }
    } else {
        if (age >= 20 && age <= 40) {
            bmiRangesToUse = bmiRangesMan;
        } else if (age > 40 && age <= 60) {
            bmiRangesToUse = bmiRangesDad;
        } else if (age > 60) {
            bmiRangesToUse = bmiRangesGpa;
        }
    }
    // if you still get hi complexity, the above code can be moved into another method.

    // fge suggests converting the below (or the bmiRanges themselves) into a RangeMap.
    if (bmi < bmiRangesToUse[0]) {
        outcome = "Underweight";
    } else if (bmi >= bmiRangesToUse[0] && bmi <= bmiRangesToUse[1]) {
        outcome = "Healthy";
    } else if (bmi > bmiRangesToUse[1] && bmi <= bmiRangesToUse[2]) {
        outcome = "Overweight";
    } else if (bmi > bmiRangesToUse[2]) {
        outcome = "Obese";
    }

    return outcome;
}
public void calculateClickHandler(视图){
布尔阳=btnM.isChecked()&&!btnF.isChecked();
年龄、英尺、英寸、磅、BMI1=任意值;
字符串结果=findoutcom(男性,BMI1);
//在某处显示结果
}
静态final int[]bmiRangesLady={21,33,39};
静态最终int[]bmiRangesMom={23,35,40};
静态最终int[]bmiRangesGma={24,36,42};
静态最终整数[]bmi Rangesman={8,19,25};
静态final int[]bmiRangesDad={11,22,27};
静态最终int[]bmiRangesGpa={13,25,30};
公共静态字符串findoutcom(布尔阳性,双bmi){
int[]bmiRangesToUse;
如果(!男性){
如果(年龄>=20岁&40岁&60岁){
bmiRangesToUse=bmiRangesGma;
}
}否则{
如果(年龄>=20岁&40岁&60岁){
bmiRangesToUse=bmiRangesGpa;
}
}
//如果您仍然具有较高的复杂性,则可以将上述代码移到另一个方法中。
//fge建议将以下范围(或BMI范围本身)转换为范围图。
如果(bmi=bmiRangesToUse[0]&&bmi bmiRangesToUse[1]&&bmi bmiRangesToUse[2]){
结果=“肥胖”;
}
返回结果;
}

尝试并坚持Java命名约定;您的
output
变量应命名为
output
。除此之外,您的IDE还抱怨您的方法的圈复杂度确实非常高。如果可以的话,而且由于这非常适合您的用例,请使用番石榴及其
范围
/
范围图
。Barett能给我举个例子吗,我跟不上您吗