3

我今天无事可做,所以我决定尝试创建一个简单的单位转换器。到目前为止,它运行良好,但由于我使用了这么多“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();
        }
    }
}
4

1 回答 1

1

面向对象的方法将摆脱这些if (lst.SelectedItem) ==陈述。

考虑这个代码,它对每个单元都有不同的类。找不到 if 语句。

  public Form1()
  {
     InitializeComponent();

     fromList.Items.Add(new CentimeterFromMillimeterConverter());
     toList.Items.Add(new CentimeterToMillimeterConverter());
  }

  void Convert(double amount)
  {
     var from = (FromMillimeterConverter) fromList.SelectedItem;
     var to = (FromMillimeterConverter) toList.SelectedItem;
     to.Convert(from.Convert(amount));
  }

public abstract class ToMillimeterConverter
{
  public abstract double Convert(double unit);
  public override string ToString()
  {
     return GetType().Name.Replace("ToMillimeterConverter", "");
  }
}

public class CentimeterToMillimeterConverter : ToMillimeterConverter
{
  public override double Convert(double centimeters)
  {
     return 10 * centimeters;
  }
}

public abstract class FromMillimeterConverter
{
  public abstract double Convert(double unit);
  public override string ToString()
  {
     return GetType().Name.Replace("FromMillimeterConverter", "");
  }
}

public class CentimeterFromMillimeterConverter : FromMillimeterConverter
{
  public override double Convert(double centimeters)
  {
     return centimeters / 10;
  }
}
于 2012-12-24T00:31:28.397 回答