gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:36:45 24 4
gpt4 key购买 nike

我遇到了一些难题..

我有一个名为“结果”的变量,它是先前计算的变量的结果,然后附加 +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 方法期间计算变量,这可能会出现问题:/

最佳答案

最终 radio 组:

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

关于java - Android Java 使用 switch/case 来计算不同的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com