如何使用if-else语句中陈述的值?

时间:2019-02-03 17:02:05

标签: c# if-statement sharpdevelop

我最近开始研究C#,并且想制作一个具有不同单位的长度转换器。但是,似乎我不能使用if-else语句中声明的值。谁能帮我吗?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace converter
{
    public partial class MainForm : Form
    {
    public MainForm()
    {
        InitializeComponent();
    }
    void ClearBClick(object sender, EventArgs e)
    {
        convIn.Clear();
        convOut.Clear();
        cmbConv.Refresh();
    }
    void ExitBClick(object sender, EventArgs e)
    {
        this.Close();
    }
    void ConvBClick(object sender, EventArgs e)
    { double exchangeRate;

        if(cmbConv.SelectedItem.ToString() == "Miles to Kilometers")
        {
            exchangeRate = 1.60934;
        }
        else if(cmbConv.SelectedItem.ToString() == "Kilometers to Miles")
        {
            exchangeRate = 0.621371;
        }
        else if(cmbConv.SelectedItem.ToString() == "Inches to Centimeters")
        {
            exchangeRate = 0.393701;
        }
        else if(cmbConv.SelectedItem.ToString() == "Centimeters to Inches")
        {
            exchangeRate = 2.54;
        }

        double conv = Convert.ToDouble(convIn.Text);
        var conversion = conv * exchangeRate;

        var result = Convert.ToString(conversion);

        convOut.Text = result;
        }

    }
}

在使用此功能之前,我使用了一个也不起作用的结构,该结构将if-else语句用不同的方法分开。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace converter
{
    public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    void ClearBClick(object sender, EventArgs e)
    {
        convIn.Clear();
        convOut.Clear();
        cmbConv.Refresh();
    }
    void ExitBClick(object sender, EventArgs e)
    {
        this.Close();
    }
    void ConvBClick(object sender, EventArgs e)
    {           double conv = Convert.ToDouble(convIn.Text);
        var conversion = conv * exchangeRate;

        var result = Convert.ToString(conversion);

        convOut.Text = result;
    }
    void CmbConvSelectedIndexChanged(object sender, EventArgs e)
    {
         double exchangeRate;

        if(cmbConv.SelectedItem.ToString() == "Miles to Kilometers")
        {
            exchangeRate = 1.60934;
        }
        else if(cmbConv.SelectedItem.ToString() == "Kilometers to Miles")
        {
            exchangeRate = 0.621371;
        }
        else if(cmbConv.SelectedItem.ToString() == "Inches to Centimeters")
        {
            exchangeRate = 0.393701;
        }
        else if(cmbConv.SelectedItem.ToString() == "Centimeters to Inches")
        {
            exchangeRate = 2.54;
        }

     }
 }

1 个答案:

答案 0 :(得分:3)

您正在尝试在exchangeRate方法中使用名为ConvBClick的变量,但是该变量在该方法中不存在。您在CmbConvSelectedIndexChanged方法中创建了一个名称类似的变量,但是之后却什么也没做。

但是,除了代码的总体结构之外,您的CmbConvSelectedIndexChanged方法实际上什么也没做。只要选定的索引在该控件上更改,它就会执行,但不会影响其他任何东西。完全不用响应该SelectedIndexChanged事件,只需编写一个方法即可根据当前所选索引是什么而返回所需的值:

double GetExchangeRate()
{
    double exchangeRate = 0.0;

    if(cmbConv.SelectedItem.ToString() == "Miles to Kilometers")
    {
        exchangeRate = 1.60934;
    }
    // etc., the rest of your conditions

    return exchangeRate;
}

然后在您的按钮中单击,您只需调用该方法即可获取当前汇率:

double conv = Convert.ToDouble(convIn.Text);
var exchangeRate = GetExchangeRate();
var conversion = conv * exchangeRate;

var result = Convert.ToString(conversion);

convOut.Text = result;