2016-10-08 46 views
0

我的当前源代码有问题。我对此很新。这是关于BMI计算,并将通过使用alertdialog显示输出。我已经提出了条件,但在其他条件下输出始终是“您超越”,即使我的输入是合法的应该是IDEAL输出。BMI计算,使用alertdialog输出

活动没有在运行中的错误,但我有一种感觉,这是我不知道的计算问题。如果这是一个重复的问题,我真的很抱歉。

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(); 
} 
} 
}); 
} 
} 

这是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> 
+0

你的代码看起来不错,你可以举一个例子,根据你是正确的,但不通过代码工作? – Sanjeet

+0

我输入157为高度,50为重量。这将是20作为bmi(理想)。但它仍然打印为“超重” –

+0

我建议您使用调试来浏览您的代码并查看变量的值。 –

回答

0

移动高度和重量分配内的onClick()。

calculate.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v){ 
    String str1 = height.getText().toString(); 
    String str2 = weight.getText().toString(); 

    try{ 
    height1 = Float.parseFloat(str1)/100; 
    weight1 = Float.parseFloat(str2); 
    }catch(NumberFormatException nfe){ 
     } 

      result = weight1/(height1*height1); 
      String msg = ""; 
      if(result<18.5){ 
       msg = "You are UNDERWEIGHT! START EATING!"; 
      } 
      else if(result<25){ 
       msg = "You are on your IDEAL!"; 
      } 
      else{ 
       msg = "You are UNDERWEIGHT! START EATING!"; 
      } 

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

      alertDialogBuilder.setTitle("Your BMI"); 

      alertDialogBuilder 
        .setMessage(msg) 
        .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(); 
     } 
    }); 
+0

谢谢你的时间先生! –

0

你目前正在设置onCreate()内的weightheight值。当Activity第一次创建时调用onCreateonCreate仅在Activity生命周期中调用一次。欲了解更多活动信息,请点击此处:https://developer.android.com/guide/components/activities.html

要解答您的问题,除非设置默认值,否则不应将值weightheight设置为onCreate。尝试移动他们进入你的onClick方法:

calculate.setOnClickListener(new View.OnClickListener(){ 

    @Override 
    public void onClick(View v){ 
     String str1 = height.getText().toString(); 
     String str2 = weight.getText().toString(); 

     try { 
      height1 = Float.parseFloat(str1)/100; 
      weight1 = Float.parseFloat(str2); 
      result = weight1/(height1*height1); 
     } 
     catch(NumberFormatException nfe) { 

     } 
     // Continue Alert Dialog stuff down here... 

你为什么有这个问题?

当前,您的Activity开始,onCreate被调用。您正在创建对xml的引用,然后尝试设置值weightheight。但是,当onCreate被调用时,用户尚未输入任何内容到EditText中的weightheight。 因此,在此之后,假设用户确实输入了值。这些值不记录在任何地方。 然后,当用户点击Button查看他们的BMI时,您尝试计算结果。结果没有任何内容,因此您的else总是被调用。这就是为什么你继续看到你所做的输出。