开关的情况下与整数EX pression整数、情况下、pression、EX

2023-09-04 13:32:37 作者:〤烈酒红颜寞

我想使用开关的情况下而不是的if else 语句,在我所要首先检查长度串并根据,我必须利用它的情况。

i am trying to use switch case instead of If Else statement, in which i have to first check length of string and as per that i have to make cases of it.

switch (mystring.length)
{
    case <=25:
    {
        //do this
        break;
    }
    case <50:
    {
        //do this
        break;
    }
    default:
        break;
}

这是一些事情,我想这样做,但无法获得如何把&LT; 25 的情况下,前面的,因为它是不恰当的按开关的情况下的规则

This is some thing i want to do but unable to get how to put <25 in front of case because it is not appropriate as per switch case rules.

推荐答案

它总是最好使用的if / else为您的特定情况下,switch语句,你不能把条件的情况下。它看起来像你检查的范围,如果在范围是不变的,那么你可以试试下面的(如果你想使用switch语句)的。

Its always better to use if/else for your particular case, With switch statement you can't put conditions in the case. It looks like you are checking for ranges and if the range is constant then you can try the following (if you want to use switch statement).

int Length = mystring.Length;
int range = (Length - 1) / 25;
switch (range)
{
    case 0:
        Console.WriteLine("Range between 0 to 25");
        break;
    case 1:
        Console.WriteLine("Range between 26 to 50");
        break;
    case 2:
        Console.WriteLine("Range between 51 to 75");
        break;

}