Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android BMI计算,单位为厘米和千克_Java_Android - Fatal编程技术网

Java Android BMI计算,单位为厘米和千克

Java Android BMI计算,单位为厘米和千克,java,android,Java,Android,hye,我想创建BMI简单的android应用程序。我找到了这个代码,但这个代码以磅和英寸计算。但是,我的程序需要以KG和CM为单位创建。我已经试着重新编码了。但是,我仍然找不到解决办法 这是代码 package com.example.bmicalculator; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import andro

hye,我想创建BMI简单的android应用程序。我找到了这个代码,但这个代码以磅和英寸计算。但是,我的程序需要以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");
    }



}
提前感谢。

要获得以公制表示的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);
    }
}

希望有帮助

int dCentimeter=selectedValue; int feetPart=(int)((dCentimeter/2.54)/12); int inchesPart=(int)((dCentimeter/2.54)-(feetPart*12));tvMyHeight.setText(feetPart+“'”+英寸在此输入代码art+“\”英尺英寸)