简体   繁体   English

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

[英]How to use values stated in the if-else statement?

I have recently started studying C# and I wanted to make a length converter with different units. 我最近开始学习C#,并且想制作一个具有不同单位的长度转换器。 However, it seems that I cannot use values stated inside the if-else statement. 但是,似乎我不能使用if-else语句中声明的值。 Can anyone please help me? 谁能帮帮我吗?

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;
        }

    }
}

Before I used this, I used a different structure which also didn´t work, in which I separated the if-else statement in different methods. 在使用此功能之前,我使用了一个也不起作用的不同结构,在该结构中,我用不同的方法分隔了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;
        }

     }
 }

You're trying to use a variable called exchangeRate in your ConvBClick method, but that variable doesn't exist in that method. 您正在尝试在ConvBClick方法中使用一个名为exchangeRate的变量,但该变量在该方法中不存在。 You created a similarly named variable in your CmbConvSelectedIndexChanged method, but then never did anything with it. 您在CmbConvSelectedIndexChanged方法中创建了一个名称类似的变量,但是之后却什么也没做。

More to the overall structure of the code however, your CmbConvSelectedIndexChanged method doesn't really do anything. 但是,对于代码的总体结构而言,您的CmbConvSelectedIndexChanged方法实际上并没有执行任何操作。 It executes any time the selected index changes on that control, but it doesn't affect anything else. 只要选定的索引在该控件上更改,它就会执行,但不会影响其他任何东西。 Instead of responding to that SelectedIndexChanged event at all, just write a method which returns the value you want based on whatever the current selected index is: 无需编写任何响应该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;
}

Then in your button click you can just call that method to get the current exchange rate: 然后在您的按钮中单击,您可以仅调用该方法以获取当前汇率:

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

var result = Convert.ToString(conversion);

convOut.Text = result;

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

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