• 1404/10/16

تمرین - محاسبه bmi با استفاده از property و method :

//   Main Form
namespace BMICalculator
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }
        private void btnCal_Click(object sender, EventArgs e)
        {
            Bmi bmical = new Bmi(txtHeight.Text, txtWeight.Text, cmbWeight.Text, cmbHeight.Text);
            lblResult.Text = bmical.BmiCal();
        }
        private void txtWeight_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double.Parse(txtWeight.Text);
            }
            catch
            {
                txtWeight.Text = null;
                lblResult.Text = "لطفا وزن را درست وارد کنید";
            }
        }
        private void txtHeight_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double.Parse(txtHeight.Text);
            }
            catch
            {
                txtHeight.Text = null;
                lblResult.Text = "لطفا قد را درست وارد کنید";
            }
        }
    }
}
// Class

namespace BMICalculator
{
    internal class Bmi
    {
        public double Weight { get; set; }
        public double Height { get; set; }
        public bool IsFull { get; set; } = true;
        public string Message { get; set; }
        public Bmi(string height, string weight, string weightType, string heightType)
        {
            HeightConvert(height, heightType);
            WeightConvert(weight, weightType);
            BmiCal();
        }
        public String BmiCal()
        {
            if (IsFull == true)
            {
                double _result;
                _result = Weight / (Height * Height);
                _result = Math.Round(_result, 2);
                Message = Convert.ToString(_result);
                return Message;
            }
            else
            {
                return Message;
            }
        }
        public void WeightConvert(string weight, string weightType)
        {
            if (weight != "" && weightType != "")
            {
                Weight = double.Parse(weight);
                Weight = (weightType == "kg") ? Weight : (Weight * 0.454);
            }
            else
            {
                Message = "لطفا وزن خود را وارد کنید";
                IsFull = false;
            }
        }
        public void HeightConvert(string height, string heightType)
        {
            if (height != "" && heightType != "")
            {
                Height = double.Parse(height);
                Height /= 100;
                Height = (heightType == "cm") ? Height : (Height * 30.48);
            }
            else
            {
                Message = "لطفا قد خود را وارد کنید";
                IsFull = false;
            }
        }
    }
}
  • 1404/10/16
  • ساعت 02:26

سلام وقت بخیر استاد

از زحمات و آموزش عالی شما متشکرم.

در پروژه بالا تنها دستور math.round() رو از هوش مصنوعی پرسیدم چون نیاز داشتم نتیجه رو روند کنم.


  • 1404/10/16
  • ساعت 11:53

سلام 

بسیار عالی 

هیچ اشکالی نداره ، اتفاقا خوبه چون پروژه را کامل کرده 


logo-enamadlogo-samandehi