我今天无事可做,所以我决定尝试创建一个简单的单位转换器。到目前为止,它运行良好,但由于我使用了这么多“if”语句,它花费的时间太长了。我在网上搜索了用 Visual Studio 制作的单位转换器的示例,但我发现的唯一一个是用 VS Basic 制作的货币转换器,它使用在线转换器进行计算。那么有没有更快的方法来创建单位转换器?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem == listBox2.SelectedItem)
{
txtAns.Text = txtFirstUnit.Text;
}
if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Inches")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 12).ToString();
}
if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Feet")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 12).ToString();
}
if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Feet")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 3).ToString();
}
if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Yard")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 3).ToString();
}
if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Inches")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 36).ToString();
}
if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Yard")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 36).ToString();
}
if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Inches")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 63360).ToString();
}
if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Mile")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 63360).ToString();
}
if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Feet")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 5280).ToString();
}
if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Mile")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 5280).ToString();
}
if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Yard")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 1760).ToString();
}
if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Mile")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 1760).ToString();
}
if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Meters")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 39.370).ToString();
}
if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Inches")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 39.370).ToString();
}
if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Feet")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 3.2808).ToString();
}
if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Meters")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 3.2808).ToString();
}
if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Yard")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 1.0936).ToString();
}
if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Meters")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 1.0936).ToString();
}
if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Miles")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) * 0.00062137).ToString();
}
if (listBox1.SelectedItem == "Miles" && listBox2.SelectedItem == "Meters")
{
txtAns.Text = (double.Parse(txtFirstUnit.Text) / 0.00062137).ToString();
}
}
}