简体   繁体   English

在C#中将代码从if语句更改为switch语句

[英]Changing code from if-statement to switch-statement in C#

I've never used switch case instead of if/else if , and I'm wondering how to use it. 我从来没有用过switch case而不是if/else if ,而且我想知道如何使用它。 I would really appreciate the help! 我非常感谢您的帮助! The task is to put in an amount of wind in a textbox(tbVindstyrke) and the code should tell the user what amount of Watt per hour(W/t) the wind is generating, in a windmill. 任务是在文本框(tbVindstyrke)中放入一定量的风,代码应告诉用户在风车中每小时产生的瓦数(W / t)。 It should post the result in a label (lbWattprodusert). 它应将结果张贴在标签(lbWattprodusert)中。

I have got it to work with an if -statement, put as I have understood, this takes up a lot of the computers processioning power (or something). 据我了解,我已经将它与if语句一起使用,这占用了许多计算机的处理能力(或其他能力)。 So, I would like to switch it up to a switch -statement. 因此,我想将其切换为switch语句。

double Vs = 0;
private void btSjekkW_Click(object sender, EventArgs e)
{
    Vs = Convert.ToDouble(tbVindstyrke.Text);
    if (Vs >= 0 && Vs <= 2.4)
        lbWattProdusert.Text = 0 + " W/t";
    else if (Vs >= 2.5 && Vs <= 3.3)
        lbWattProdusert.Text = 2 + " W/t";
    else if (Vs >= 3.4 && Vs <= 5.4)
        lbWattProdusert.Text = 10 + " W/t";
}

switch statements work with constant values. switch语句使用常量值。

So, this is valid: 因此,这是有效的:

var val = 2;
switch (val)
{
    case 1:
        // Do something if val is 1.
        break;
    case 2:
        // Do something if val is 2.
        break;
    default:
        // Do something for all values of val other than 1 or 2.
        break;
}

But you want to convert an if-else that deals with ranges. 但是您想转换一个if-else来处理范围。 That too with double ranges. double范围也是如此。 This is not possible since a switch doesn't allow you to work with a range. 这是不可能的,因为switch不允许您使用范围。

If your ranges are int , you could hypothetically write a case for each value in the rage, but that makes absolutely no sense. 如果范围是int ,则可以假设为范围内的每个值写一个case ,但这绝对没有意义。

Say, you want to do something if the value is between int 1-3, and something else if it's between 4-6. 说,如果值介于int 1-3之间,并且要在4-6之间,则要执行其他操作。 You could write something like the follwing, but that would be nonsensicle. 您可以写类似下面的内容,但是那太荒谬了。 You'd be better off sticking to an if-else . 您最好坚持if-else

var val = 2;
switch (val)
{
    case 1:
    case 2:
    case 3:
        // Do something if val is between 1-3.
        break;
    case 4:
    case 5:
    case 6:
        // Do something if val is between 4-6.
        break;
    default:
        // Do something for all other values of val
        break;
}

A switch statement is useful when you want to choose between many options based on a single value. 当您要基于单个值在多个选项之间进行选择时,switch语句很有用。 When you want to work against ranges, as you have done in your example, an if-statement is needed. 如您在示例中所做的那样,当您要针对范围进行工作时,需要一个if语句。

Here's an example of a switch statement... you can see that you specify the value to switch on at the start and you can then perform different actions based on equality. 这是一个switch语句的示例...您可以看到,您指定了要在开始时打开的值,然后可以基于相等性执行不同的操作。

string color = "red";

switch (color)
{
    case "red":
        // do something
        break;
    case "green":
        // do something
        break;
    case "blue":
        // do something
        break;
    default:
        throw new ColorUnknownException(color);
}

Very often, a switch-statement is a sign of a missing design pattern... but that is a conversation for another day. 很多时候,切换语句是缺少设计模式的标志……但这是又一天的对话。

switch doesn't work with float and double values . switch不适用于float和double values However, as all your range limits are multiples of 0.3, you can use the trick of dividing the value by 0.3 to achieve what you seek as follows: 但是,由于所有范围限制都是0.3的倍数,因此您可以使用将值除以0.3的技巧来实现以下目标:

int Vi;
double Vs = 0;
private void btSjekkW_Click(object sender, EventArgs e)
{
    Vs = Convert.ToDouble(tbVindstyrke.Text);
    Vi = (int)(Vs / 0.3);
    switch (caseSwitch)
    {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
            lbWattProdusert.Text = 0 + " W/t";
            break;
        case 9:
        case 10:
        case 11:
            lbWattProdusert.Text = 2 + " W/t";
            break;
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
            lbWattProdusert.Text = 10 + " W/t";
          default:
            // what to do if Vs > 5.4
            break;
      }
}

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

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