|
namespace BMI计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
float height, weight;
float bmi=0;
try
{
height = Single.Parse(textBox1.Text);
}
catch (System.FormatException)
{
MessageBox.Show("身高一栏,请输入实数");
return;
}
catch (System.OverflowException)
{
MessageBox.Show("身高一栏,数字超出范围");
return;
}
try
{
weight = Single.Parse(textBox2.Text);
}
catch (System.FormatException)
{
MessageBox.Show("体重一栏,请输入实数");
return;
}
catch (System.OverflowException)
{
MessageBox.Show("体重一栏,数字超出范围");
return;
}
try
{
bmi = weight / (height * height);
}
catch (System.OverflowException)
{
MessageBox.Show("计算的BMI超出浮点数表示范围");
return;
}
textBox3.Text = bmi.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
}
|
|