2013-04-18 1 views
-1

예, 나는 BMI 간단한 안드로이드 응용 프로그램을 만들고 싶습니다. 나는이 코드를 발견했지만이 코드는 LBS와 INCH로 계산합니다. 하지만, 내 프로그램은 KG와 CM에서 만들어야합니다. 벌써 다시 녹음하려고합니다. 하지만, 나는 여전히 솔루션을 찾을 수 없습니다.CM 및 KG에서 안드로이드 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 단위로 나눌 필요가 있습니다. 따라서 높이를 cm로 요구하는 경우 계산을 수행하기 전에 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); 
    } 
} 

희망 하시겠습니까?