Advertisement
mmayoub

מחשבון BMI, פעולות JAVA

Sep 17th, 2022
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | Software | 0 0
  1. package com.example.mybmicalculator16092022;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7.  
  8.  
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.     // 1
  16.     private EditText etWeight, etHight;
  17.     private Button btnCalculate;
  18.     private TextView tvResult;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         // 0
  24.         setContentView(R.layout.activity_main);
  25.  
  26.         // 3
  27.         etWeight = findViewById(R.id.etWeight);
  28.         etHight = findViewById(R.id.etHight);
  29.         btnCalculate = findViewById(R.id.btnCalculate);
  30.         tvResult = findViewById(R.id.tvResult);
  31.  
  32.         // 4 init
  33.         tvResult.setText("");
  34.     }
  35.  
  36.     public void calculateBmi(View view) {
  37.         String weightStr = etWeight.getText().toString();
  38.         String hightStr = etHight.getText().toString();
  39.  
  40.         double weight = Double.parseDouble(weightStr);
  41.         double hight = Double.parseDouble(hightStr);
  42.  
  43.         double bmi = Math.round(weight / (hight * hight));
  44.  
  45.         if (bmi < 18.5) {
  46.             tvResult.setText("The result is " + bmi + "\nUnderWeight");
  47.             tvResult.setTextColor(Color.CYAN);
  48.         } else if (bmi < 25) {
  49.             tvResult.setText("The result is " + bmi + "\nNormal");
  50.             tvResult.setTextColor(Color.GREEN);
  51.         } else if (bmi < 30) {
  52.             tvResult.setText("The result is " + bmi + "\nOverWeight");
  53.             tvResult.setTextColor(Color.YELLOW);
  54.         } else {
  55.             tvResult.setText("The result is " + bmi + "\nObese");
  56.             tvResult.setTextColor(Color.RED);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement