【问题标题】:Calculating Measurement Conversions计算测量转换
【发布时间】:2016-06-06 04:13:12
【问题描述】:

我是 C# 的新手,正在学习我正在苦苦挣扎的课程。

任务是使用文本框值 (valuetextbox) 创建一个转换应用程序,并有 2 个保存英寸、英尺和码测量值的列表框。目标是根据在第一个列表框中选择的测量单位 (selectedinitialunit) 转换值,并根据在第二个列表框中选择的测量单位 (selectedconvertedunit) 在标签中张贴一个值。我错过了如何根据输入到 valuetextbox 中的值在我的 selectedinitialunit 和我 selectedconvertedunit 之间进行比较(数学)。

namespace Measurement_Conversion
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void convertedValueLabel_Click(object sender, EventArgs e)
    {

    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void resetButton_Click(object sender, EventArgs e)
    {
        valueTextBox.Text = "";
        convertedValueLabel.Text = "";
        DesiredUnitBox.SelectedIndex = -1;
        InitialUnitBox.SelectedIndex = -1;
        valueTextBox.Focus();
    }

    private void convertButton_Click(object sender, EventArgs e)
    {

        //Declare Variables
        decimal initialvalue;
        decimal convertedvalue;
        string selectedInitialUnit;
        string selectedConvertedUnit;



        //Grab Initial Value
        initialvalue = int.Parse(valueTextBox.Text);

        //Determining Initial Unit has been selected.
        if (InitialUnitBox.SelectedIndex != -1)
        {
            selectedInitialUnit = InitialUnitBox.SelectedItem.ToString();

        }
        else { MessageBox.Show("Please select an Initial Unit of measurement."); }


        //Determining Desired Conversion Unit has been selected
        if (DesiredUnitBox.SelectedIndex != -1)
        {
            selectedConvertedUnit = DesiredUnitBox.SelectedItem.ToString();
        }
        else { MessageBox.Show("Please select a desired conversion unit of measurement."); }

        //Switch Statement
        switch (selectedInitialUnit)
        {
            case "Inches":
                convertedValueLabel.Text = ()
                break;
            case "Feet":
                break;
            case "Yards":
                break;


        }
    }



}

}

我写在纸上的内容是这样说的——这不是代码,只是我的思考过程:

if selectedinitialunit = INCHES and selectedconvertedunit = INCHES
then messagebox.show("Please select a different selectedconvertedunit");

else selectedinitialunit = INCHES and selectedconvertedunit = FEET
then convertedvaluelabel.Text = initialvalue/12;

else selectedinitialunit = INCHES and selectedconvertedunit = YARDS
then convertedvaluelabel.Text = initialvalue/36;

对于其他选定的初始单位,依此类推。

非常感谢您的帮助,我希望我不会在这里到处乱跑。

提前谢谢你!

【问题讨论】:

    标签: c# listbox


    【解决方案1】:

    你可以这样写代码-

            string selectedUnit = selectedinitialunit.SelectedItem.ToString();
            string convertedUnit = selectedconvertedunit.SelectedItem.ToString();
            double valueToConvert = double.Parse(initialvalue.Text);
            if (selectedUnit.Equals(convertedUnit))
            {
                MessageBox.Show("Please select a different selectedconvertedunit");
            }
            else
            {
                switch (selectedUnit)
                {
                    case "INCHES":
                        switch (convertedUnit)
                        {
                            case "FEET":
                                convertedvaluelabel.Text = (valueToConvert / 12.0).ToString();
                                break;
                            case "YARDS":
                                convertedvaluelabel.Text = (valueToConvert / 36.0).ToString();
                                break;
                        }
                        break;
                }
            }
    

    您始终可以在代码中添加某种异常和错误处理以及验证。

    【讨论】:

    • 这是我的解决方案。非常感谢,安基特。现在我将构建其余的测量转换。
    • 感谢 DJDJ23。继续询问有关堆栈溢出的问题并投票并接受有用的答案
    【解决方案2】:

    最简单的方法是选择一个“基础”度量并转换为该度量,然后再从该度量中转换。

    它甚至可以是米或毫米。这样,您只需要 2(例如,如果您选择英寸)或 3 个乘数即可。

    const double InchesInFoot = 12;
    const double InchesInYard = InchesInFoot * 3;
    
    // step 1
    double valueInches = input * constant above according to input measurement;
    
    // step 2
    double result = valueInches / constant according to output measurement;
    

    你也可以像矩阵一样使用

            double[,] factors = new double[3, 3] {
                { 1.0, 1.0/12, 1.0/12/3 },
                { 12.0, 1, 1.0/3 },
                { 12*3,3,1 }
            };
    
            const int Inches = 0;
            const int Feet = 1;
            const int Yards = 2;
    
            var result = factors[Yards, Inches] * 1;
    

    这将使它更容易阅读,但您需要预先做更多的数学运算。

    我想这需要你理解多维数组和事物,以及比要求更多的数学知识。

    【讨论】:

      【解决方案3】:

      你可以使用:

      //Switch Statement
      switch (selectedInitialUnit + "->" + selectedconvertedunit)
      {
          case "Inches->Feet":
              convertedValueLabel.Text = ()
              break;
          case "Feet->Inches":
              break;
          case "Yards->Feet":
              break;
          case "Yards->Yards":
          case "Feet->Feet":
              Messagebox.Show("Please select a different selectedconvertedunit");
              break;
      }
      

      另一种方法是直接使用SelectedIndex

      这里贴出来给大家看看,不推荐使用下面的代码。

      例如,如果InitialUnitBoxDesiredUnitBox 的数字项

      switch (InitialUnitBox.SelectedIndex * 10 + DesiredUnitBox.SelectedIndex)
      {
          case 0:       // = 0 * 10 + 0: first item of both box
          case 11:      // Same index both box
          case 22:      // Same index both box
              Messagebox.Show("Please select a different selectedconvertedunit");
              break;
          case 10:      // = 1 * 10 + 0: second item of InitialUnitBox and first item of DesiredUnitBox
              break;
          case 11:      // = 1 * 10 + 1
              break;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-17
        • 1970-01-01
        • 2013-07-12
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 2013-01-19
        • 2011-08-04
        • 1970-01-01
        相关资源
        最近更新 更多