开关的情况下与整数EX pression [英] switch case with integer expression

查看:121
本文介绍了开关的情况下与整数EX pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用开关的情况下而不是的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;

}

这篇关于开关的情况下与整数EX pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆