-1

嘿,我想创建 BMI 简单的 android 应用程序。我找到了这段代码,但这段代码以 LBS 和英寸计算。但是,我的程序需要在 KG 和 CM 中创建。我已经尝试重新编码。但是,我仍然找不到解决方案。

这是代码。

package com.example.bmicalculator;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.EditText;
    import android.widget.Button;
    import android.view.View;

    public class BMICalculatorActivity extends Activity {

    private EditText txtheight;
    private EditText txtweight;
    private TextView txtresult;
    private Button btncalculate;
    private double bmi = 0;
    private double valueheight = 0;
    private double valueweight = 0;
    private String resulttext;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initControls();
        /*TextView tv = new TextView(this);
        tv.setText("BMI Calculator");
        setContentView(tv);*/

   }


    private void initControls() {
        txtheight = (EditText)findViewById(R.id.txtheight);
        txtweight = (EditText)findViewById(R.id.txtweight);
        txtresult = (TextView)findViewById(R.id.txtresult);
        btncalculate = (Button)findViewById(R.id.btncalculate);
        //btnreset = (Button)findViewById(R.id.btnreset);
        btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
        //btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
    }

    private void calculate()    {
        valueheight =Double.parseDouble(txtheight.getText().toString());
        valueweight =Double.parseDouble(txtweight.getText().toString());
        bmi = (valueweight / (valueheight * valueheight));
        //txttipamount.setText(Double.toString(bmi));
        if (bmi >= 30) { /* obese */
            resulttext = "Your BMI of " + Double.toString(bmi) + " is OBESE.";
            txtresult.setText(resulttext);
        } else if (bmi >= 25) {
            resulttext = "Your BMI of " + Double.toString(bmi) + " is OVERWEIGHT.";
            txtresult.setText(resulttext);
        } else if (bmi >= 18.5) {
            resulttext = "Your BMI of " + Double.toString(bmi) + " is IDEAL.";
            txtresult.setText(resulttext);
        } else {
            resulttext = "Your BMI of " + Double.toString(bmi) + " is UNDERWEIGHT.";
            txtresult.setText(resulttext);
        }
    }
    private void reset()
    {
        txtresult.setText("Your BMI is unknown.");
        txtheight.setText("0");
        txtweight.setText("0");
    }



}

提前致谢。

4

2 回答 2

6

要获得以公制为单位的 BMI(身体质量指数),您需要用以千克为单位的体重除以以米为单位的身高^2。因此,如果您要求以厘米为单位的身高,请在进行计算之前将其除以 100。例子:

private void calculate()    {
    valueheight =Double.parseDouble(txtheight.getText().toString());
    valueweight =Double.parseDouble(txtweight.getText().toString());
    Double valueheightmeters;

    valueheightmeters = valueheight / 100; // Converting to meters.
    bmi = (valueweight / (valueheightmeters * valueheightmeters));

    //txttipamount.setText(Double.toString(bmi));
    if (bmi >= 30) { /* obese */
        resulttext = "Your BMI of " + Double.toString(bmi) + " is OBESE.";
        txtresult.setText(resulttext);
    } else if (bmi >= 25) {
        resulttext = "Your BMI of " + Double.toString(bmi) + " is OVERWEIGHT.";
        txtresult.setText(resulttext);
    } else if (bmi >= 18.5) {
        resulttext = "Your BMI of " + Double.toString(bmi) + " is IDEAL.";
        txtresult.setText(resulttext);
    } else {
        resulttext = "Your BMI of " + Double.toString(bmi) + " is UNDERWEIGHT.";
        txtresult.setText(resulttext);
    }
}

希望有帮助!

于 2013-04-18T01:34:35.020 回答
-1

int dCentimeter = selectedValue; int feetPart = (int) ((dCentimeter / 2.54) / 12); int 英寸Part = (int) ((dCentimeter / 2.54) - (feetPart * 12)); tvMyHeight.setText(feetPart + "'" + inchesPenter 代码 hereart + "\" ft in");

于 2018-07-10T09:21:44.773 回答