错误方法过于复杂,无法通过数据流算法进行分析 [英] Error Method is too complex to analyze by data flow algorithm

查看:69
本文介绍了错误方法过于复杂,无法通过数据流算法进行分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the method that is causing the issue. I am creating a BMI calculator that uses Age, weight and height to calculate the end result. I'm not sure if my logic is wrong or if there's another issue going on

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);
        }
    }
}

}

推荐答案

您的逻辑很好.Android Studio(或其他功能?)只是抱怨说太多分支无法计算该方法的数值复杂度.

Your logic is fine. Android Studio (or whatever?) is just complaining that there are too many branches to calculate a numeric complexity for the method.

如果您想减轻错误,请创建一个新的类以将BMI值(以及性别的布尔值或枚举(我假设btnM就是这个意思))解析为单独的类或方法.

If you want to alleviate the error, create a new class to resolve the BMI value (and perhaps a boolean or enum for gender (I assume that's what btnM means)) into a separate class or method.

---编辑

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

You can see in the below code that the redundant range checking code is eliminated by first finding the appropriate bounds, then evaluating the bounds and BMI1 into an outcome.

在制作这些文件的过程中,您还会注意到您的祖父范围内有一个错误(13/14),并且没有针对19岁或80岁以上年龄段的计算.

In the process of making these, you will also notice you had a bug in your grandpa ranges (13/14) and have no calculations for ages 19- or 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;
}

这篇关于错误方法过于复杂,无法通过数据流算法进行分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆