java 错误方法过于复杂,无法通过数据流算法进行分析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29979353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:12:54  来源:igfitidea点击:

Error Method is too complex to analyze by data flow algorithm

javaandroidcss

提问by connor

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

这是导致问题的方法。我正在创建一个 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);
        }
    }
}

}

回答by Barett

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.

你的逻辑没问题。Android Studio(或其他什么?)只是抱怨有太多分支来计算该方法的数字复杂度。

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.

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

--- EDIT

- - 编辑

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.

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

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+.

在制作这些的过程中,您还会注意到您的祖父范围(13/14)有一个错误,并且没有对 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;
}