Android Java使用switch / case来计算不同的百分比

时间:2013-05-07 15:22:55

标签: java android math percentage

我有点难题..

我有一个名为“results”的变量,它是先前计算的变量的结果,然后追加+ 10%,+ 20%,0%(与它相同......)-10%等依赖在用户选择的单选按钮上,麻烦的是数据仅在用户单击按钮时计算。 到目前为止我的按钮工作正常:

public void onClick(View v) {
        switch (v.getId()) {

        case R.id.btnCalcCalories:
            double weight = Integer.parseInt(etWeight.getText().toString());
            double bodyfat = Integer.parseInt(etBodyfat.getText().toString());
            ;
            lbm = weight * (100 - bodyfat) / 100;
            bmr = 370 + (21.6 * lbm);
            maintCals = bmr * actLevel;

            maintCalories.setText("Calories need to maintain current bodyweight: " + String.valueOf(maintCals));
            lbmResult.setText("Your Lean Body Mass is " + String.valueOf(lbm)
                    + "Kg");
            bmrResult.setText("Your Base Metabolic rate is "
                    + String.valueOf(bmr) + " calories");
            calorieResult.setText("Your Daily calorie goal is " + String.valueOf(calResult) + " calories");

虽然我需要获得更多用户希望使用这样的无线电组丢失/获得/保持体重:

public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
        case R.id.radLW:
            setGPercentage = 10 * maintCals/100;
            calResult = maintCals - setGPercentage;
            break;
        case R.id.radLWF:
            setGPercentage = 20 * maintCals/100;
            calResult = maintCals - setGPercentage;
            break;
        case R.id.radM:
            setGPercentage = 0;
            break;
        case R.id.radGW:
            setGPercentage = 10 * maintCals/100;
            calResult = maintCals + setGPercentage;
            break;
        case R.id.radGWF:
            setGPercentage = 20 * maintCals/100;
            calResult = maintCals + setGPercentage;
            break;
        }

为了澄清,我需要根据用户选择的内容计算'calResult',显然如果仅在onClick方法中计算变量,这可能是一个问题:/

2 个答案:

答案 0 :(得分:1)

最终的无线电组:

public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
        case R.id.radLW:
            setGPercentage = 10 * maintCals/100;
            finalGPercentage = maintCals - setGPercentage;
            break;
        case R.id.radLWF:
            setGPercentage = 20 * maintCals/100;
            finalGPercentage = maintCals - setGPercentage;
            break;
        case R.id.radM:
            setGPercentage = 0;
            break;
        case R.id.radGW:
            setGPercentage = 10 * maintCals/100;
            finalGPercentage = maintCals + setGPercentage;
            break;
        case R.id.radGWF:
            setGPercentage = 20 * maintCals/100;
            finalGPercentage = maintCals + setGPercentage;
            break;
        }

和onClick:

case R.id.btnCalcCalories:
            double weight = Integer.parseInt(etWeight.getText().toString());
            double bodyfat = Integer.parseInt(etBodyfat.getText().toString());
            ;
            lbm = weight * (100 - bodyfat) / 100;
            bmr = 370 + (21.6 * lbm);
            maintCals = bmr * actLevel;

            maintCalories.setText("Calories need to maintain current bodyweight: " + String.valueOf(maintCals));

            lbmResult.setText("Your Lean Body Mass is " + String.valueOf(lbm)
                    + "Kg");
            bmrResult.setText("Your Base Metabolic rate is "
                    + String.valueOf(bmr) + " calories");
            calorieResult.setText("Your Daily calorie goal is " + String.valueOf(finalGPercentage) + " calories");
            break;

我只需要另一个变量来进行计算:D

答案 1 :(得分:0)

您的问题并不完全清楚,但我认为您需要做的是为您的活动设置某种初始化,因此您的calResult在用户进行选择之前会有一个计算的(默认)值。

请查看Activity class的文档,以便更好地了解Activty生命周期以及与onCreate等初始化相关的方法。