2013-04-18 57 views
-1

hye,我想創建BMI簡單的android應用程序。我發現這個代碼,但這個代碼在LBS和英寸計算。但是,我的計劃需要在KG和CM中創建。我已經嘗試重新編碼。但是,我仍然找不到解決方案。CM和KG的Android BMI計算

這是代碼。

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

要獲得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); 
    } 
} 

希望有所幫助!