Ich habe ein Problem mit meinem aktuellen Quellcode. Ich bin damit ziemlich neu. Es geht um die BMI-Berechnung und zeigt die Ausgabe mithilfe von alertdialog an. Ich habe die Bedingung bereits gestellt, aber die Ausgabe ist immer "Sie sind übergewichtig" auf sonst Bedingung, obwohl meine Eingabe legitim sein sollte IDEAL Ausgabe.BMI-Berechnung, Ausgabe mit Alertdialog
Die Aktivität hat keinen Fehler beim Laufen, aber ich habe das Gefühl, dass es das Berechnungsproblem ist, das ich nicht kenne. Es tut mir wirklich leid, wenn das eine doppelte Frage ist.
public class BMIcalcu extends Activity{
EditText weight, height;
final Context context = this;
Button calculate;
float result, height1, weight1;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_calcu);
height = (EditText)findViewById(R.id.editText1);
weight = (EditText)findViewById(R.id.editText2);
calculate = (Button)findViewbyId(R.id.btn_bmi);
String str1 = height.getText().toString();
String str2 = weight.getText().toString();
try{
height1 = Float.parseFloat(str1)/100;
weight1 = Float.parseFloat(str2);
}catch(NumberFormatException nfe){
}
calculate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
result = weight1/(height1*height1);
if(result<18.5){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage("You are UNDERWEIGHT! START EATING!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else if(result<25){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage("You are on your IDEAL!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage("You are OVERWEIGHT! START EXERCISING!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
}
Dies ist die XML-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_gravity="center"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:inputType="text|number"
android:hint="Height (Centimeter)"
android:ems="10"
android:id="@+id/editText1" />
<EditText
android:layout_gravity="center"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:inputType="text|number"
android:hint="Weight (Kilogram)"
android:ems="10"
android:id="@+id/editText2"/>
<Button
android:id="@+id/btn_bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/editText2"
android:text="Calculate"/>
</LinearLayout>
Ihr Code sieht gut aus, können Sie ein Beispiel angeben, das Ihrer Meinung nach korrekt ist, aber nicht über Code funktioniert? – Sanjeet
I Eingabe 157 als Höhe, 50 als Gewicht. Es wäre 20 als bmi (ideal). aber es druckt immer noch als "übergewichtig" –
Ich schlage vor, dass Sie die Debugger verwenden, um durch Ihren Code zu gehen und die Werte Ihrer Variablen anzuzeigen. –