繁体   English   中英

C#距离转换器

[英]C# Distance converter

我正在为距离转换器使用C#Forms应用程序,这是我现在拥有的代码,我需要声明toDistance,但我不太确定如何用距离转换的结果声明它。 如果可以的话,请帮忙,我现在很新

private void btnConvert_Click(object sender, EventArgs e)
{
    float fromDistance;
    float toDistance;

    fromDistance = int.Parse(distanceinput.Text);
    string measureInput = fromList.Items[fromList.SelectedIndex].ToString();
    string measureOutput = toList.Items[toList.SelectedIndex].ToString();
    distanceOutput.Text = toDistance.ToString();

    switch (measureInput)
    {
        case "Yard":
            switch (measureOutput)
            {
                case "Yard":
                    toDistance = fromDistance;
                    break;
                case "Foot":
                    toDistance = fromDistance * 3;
                    break;
                case "Inches":
                    toDistance = fromDistance * 3 * 12;
                    break;
            }
            break;
        case "Foot":
            switch (measureOutput)
            {
                case "Foot":
                    toDistance = fromDistance;
                    break;
                case "Yard":
                    toDistance = fromDistance / 3;
                    break;
                case "Inch":
                    toDistance = fromDistance * 12;
                    break;
            }
            break;
        case "Inch":
            switch (measureOutput)
            {
                case "Inch":
                    toDistance = fromDistance;
                    break;
                case "Foot":
                    toDistance = fromDistance / 12;
                    break;
                case "Yards":
                    toDistance = fromDistance / (3 * 12);
                    break;
            }
            break;
    }
}

您是否尝试移动distanceOutput.Text = toDistance.ToString(); 到功能结束?

 private void btnConvert_Click(object sender, EventArgs e)
        {
            float fromDistance;
            float toDistance;

            fromDistance = int.Parse(distanceinput.Text);
            string measureInput = fromList.Items[fromList.SelectedIndex].ToString();
            string measureOutput = toList.Items[toList.SelectedIndex].ToString();


            switch (measureInput)
            {
                case "Yard":
                    switch (measureOutput)
                    {
                        case "Yard":
                            toDistance = fromDistance;
                            break;
                        case "Foot":
                            toDistance = fromDistance * 3;
                            break;
                        case "Inches":
                            toDistance = fromDistance * 3 * 12;
                            break;
                    }
                    break;
                case "Foot":
                    switch (measureOutput)
                    {
                        case "Foot":
                            toDistance = fromDistance;
                            break;
                        case "Yard":
                            toDistance = fromDistance / 3;
                            break;
                        case "Inch":
                            toDistance = fromDistance * 12;
                            break;
                    }
                    break;
                case "Inch":
                    switch (measureOutput)
                    {
                        case "Inch":
                            toDistance = fromDistance;
                            break;
                        case "Foot":
                            toDistance = fromDistance / 12;
                            break;
                        case "Yards":
                            toDistance = fromDistance / (3 * 12);
                            break;
                    }
                    break;
            }
    distanceOutput.Text = toDistance.ToString();
        }

这行:

distanceOutput.Text = toDistance.ToString();

并不是说:“控制distanceOutput的文本应始终遵循无论是‘’toDistance的”的ToString。

相反,它的意思是“做正确的现在 ”(如:此刻正在执行的线)。 它没有保持联系。

因此,解决方案是将该行移动到(外部)switch语句之后该语句中您将其计算为toDistance。

但是,您可能需要对其进行初始化( float toDistance = 0f; ),否则编译器会抱怨您正在尝试使用未初始化的变量。 它看不到您已在switch es中使用了所有可能的值。

切换后,您需要将最终分配声明移至。

另外,我不会使用嵌套开关。 很难读取和调试。 我会将输入转换为最小的单位(在您的情况下为英寸)。 然后,我将最小的单位度量转换为用户要求的任何输出单位:

float inputInches = 0;

//Convert input to inches
switch(measureInput)
{
    case "Yard":
        inputInches = fromDistance * 3 * 12;
        break;
    case "Foot":
        inputInches = fromDistance * 12;
        break;
    default:
        inputInches = fromDistance;
        break;
}

//Convert output to desired format
switch(measureOutput)
{
    case "Yard":
        toDistance = inputInches / 3 / 12;
        break;
    case "Foot":
        toDistance = inputInches / 12;
        break;
    default:
        toDistance = inputInches;
        break;
}

distanceOutput.Text = toDistance.ToString();

需要考虑的一些其他事项:

  • 文本比较比较混乱。 考虑使用输入类型( InchFootYard等)创建一个enum
  • Button事件中的编码也很混乱。 考虑将此转换代码移至其自己的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM