C# 如何在数字范围的函数中优雅地赋值?

C# 如何在数字范围的函数中优雅地赋值?,c#,numbers,range,C#,Numbers,Range,我正在寻找一种优雅的方法来分配属于特定范围的数字函数中的值 例如,使用数字X时,优雅方式将返回: “a”-如果X在0和1000之间 “b”-如果X在1000和1500之间 依此类推(但定义了固定数量的间隔) 我说的优雅是指比优雅更吸引人的东西 if ((x => interval_1) && (x < interval_2)) class_of_x = 'a'; else if ((x => interval_2) && (x <

我正在寻找一种优雅的方法来分配属于特定范围的数字函数中的值

例如,使用数字X时,
优雅方式将返回:

  • “a”-如果X在0和1000之间
  • “b”-如果X在1000和1500之间
  • 依此类推(但定义了固定数量的间隔)
我说的优雅是指比优雅更吸引人的东西

if ((x => interval_1) && (x < interval_2))
    class_of_x = 'a';
else if ((x => interval_2) && (x < interval_3))
    class_of_x = 'b';
...
我讨厌看到这么多的如果。 此外,间隔值可以存储在集合中(可能这会帮助我消除ISs?),而不是间隔1、间隔2等所必需的


在寻找上述问题的解决方案时,这个问题产生了一些启发。

如果我的评论是正确的,那么你的第一个If语句有很多不必要的检查,如果它不小于间隔2,那么它必须大于或等于,因此:

if((x => i1) && (x < i2))
else if(x < i3)
else if(x < i4)...
if((x=>i1)和&(x

当发现一个“true”参数时,if语句的其余部分是无关的,只要您的条件符合要求,这应该适合您的需要

如果我的评论是正确的,那么您的第一个if语句有很多不必要的检查,如果它不小于间隔2,那么它必须大于或等于,因此:

if((x => i1) && (x < i2))
else if(x < i3)
else if(x < i4)...
if((x=>i1)和&(x

当找到一个“true”参数时,if语句的其余部分就不相关了,只要您的条件符合顺序,这应该适合您的需要

您可以创建扩展方法:

public static class IntExtensions
{
    // min inclusive, max exclusive
    public static bool IsBetween(this int source, int min, int max)
    {
        return source >= min && source < max
    }
}
公共静态类扩展
{
//最小值包含,最大值不包含
公共静态bool IsBetween(此int源、int最小值、int最大值)
{
返回源>=min&&source
然后

// Item1 = min, Item2 = max, Item3 = character class
IList<Tuple<int, int, char>> ranges = new List<Tuple<int, int, char>>();
// init your ranges here
int num = 1;
// assuming that there certainly is a range which fits num,
// otherwise use "OrDefault"
// it may be good to create wrapper for Tuple, 
// or create separate class for your data
char characterClass = ranges.
                        First(i => num.IsBetween(i.Item1, i.Item2)).Item3;
//Item1=min,Item2=max,Item3=character类
IList ranges=新列表();
//在这里初始化你的范围
int num=1;
//假设确实存在一个适合num的范围,
//否则使用“OrDefault”
//可以为元组创建包装器,
//或者为数据创建单独的类
characterClass=范围。
首先(i=>num.IsBetween(i.Item1,i.Item2)).Item3;

您可以创建扩展方法:

public static class IntExtensions
{
    // min inclusive, max exclusive
    public static bool IsBetween(this int source, int min, int max)
    {
        return source >= min && source < max
    }
}
公共静态类扩展
{
//最小值包含,最大值不包含
公共静态bool IsBetween(此int源、int最小值、int最大值)
{
返回源>=min&&source
然后

// Item1 = min, Item2 = max, Item3 = character class
IList<Tuple<int, int, char>> ranges = new List<Tuple<int, int, char>>();
// init your ranges here
int num = 1;
// assuming that there certainly is a range which fits num,
// otherwise use "OrDefault"
// it may be good to create wrapper for Tuple, 
// or create separate class for your data
char characterClass = ranges.
                        First(i => num.IsBetween(i.Item1, i.Item2)).Item3;
//Item1=min,Item2=max,Item3=character类
IList ranges=新列表();
//在这里初始化你的范围
int num=1;
//假设确实存在一个适合num的范围,
//否则使用“OrDefault”
//可以为元组创建包装器,
//或者为数据创建单独的类
characterClass=范围。
首先(i=>num.IsBetween(i.Item1,i.Item2)).Item3;

首先,定义一个小类来保存包含的最大值,以及用于该波段的相应值:

sealed class Band
{
    public int  InclusiveMax;
    public char Value;
}
然后声明一个
波段
数组,该数组指定每个波段和循环使用的值,以查找任何输入的相应波段值:

public char GetSetting(int input)
{
    var bands = new[]
    {
        new Band {InclusiveMax = 1000, Value = 'a'},
        new Band {InclusiveMax = 1500, Value = 'b'},
        new Band {InclusiveMax = 3000, Value = 'c'}
    };

    char maxSetting = 'd';

    foreach (var band in bands)
        if (input <= band.InclusiveMax)
            return band.Value;

    return maxSetting;
}
public char GetSetting(int输入)
{
var波段=新[]
{
新带{InclusiveMax=1000,值='a'},
新带{InclusiveMax=1500,值='b'},
新带{InclusiveMax=3000,值='c'}
};
char maxSetting='d';
foreach(带中的var带)

如果(输入首先,定义一个小类来保存包含的最大值,以及用于该波段的相应值:

sealed class Band
{
    public int  InclusiveMax;
    public char Value;
}
然后声明一个
波段
数组,该数组指定每个波段和循环使用的值,以查找任何输入的相应波段值:

public char GetSetting(int input)
{
    var bands = new[]
    {
        new Band {InclusiveMax = 1000, Value = 'a'},
        new Band {InclusiveMax = 1500, Value = 'b'},
        new Band {InclusiveMax = 3000, Value = 'c'}
    };

    char maxSetting = 'd';

    foreach (var band in bands)
        if (input <= band.InclusiveMax)
            return band.Value;

    return maxSetting;
}
public char GetSetting(int输入)
{
var波段=新[]
{
新带{InclusiveMax=1000,值='a'},
新带{InclusiveMax=1500,值='b'},
新带{InclusiveMax=3000,值='c'}
};
char maxSetting='d';
foreach(带中的var带)

如果(输入创建一个Interval类并使用LINQ:

public class Interval
{
    public string TheValue { get; set; }
    public int Start { get; set; }
    public int End { get; set; }

    public bool InRange(int x)
    {
        return x >= this.Start && x <= this.End;
    }
}

public void MyMethod()
{
    var intervals = new List<Interval>();

    // Add them here...

    var x = 3213;
    var correctOne = intervals.FirstOrDefault(i => i.InRange(x));

    Console.WriteLine(correctOne.TheValue);
}
公共类间隔
{
公共字符串值{get;set;}
public int Start{get;set;}
公共int End{get;set;}
范围内的公共布尔(int x)
{
返回x>=this.Start&&x i.InRange(x));
Console.WriteLine(正确的1.TheValue);
}

创建一个Interval类并使用LINQ:

public class Interval
{
    public string TheValue { get; set; }
    public int Start { get; set; }
    public int End { get; set; }

    public bool InRange(int x)
    {
        return x >= this.Start && x <= this.End;
    }
}

public void MyMethod()
{
    var intervals = new List<Interval>();

    // Add them here...

    var x = 3213;
    var correctOne = intervals.FirstOrDefault(i => i.InRange(x));

    Console.WriteLine(correctOne.TheValue);
}
公共类间隔
{
公共字符串值{get;set;}
public int Start{get;set;}
公共int End{get;set;}
范围内的公共布尔(int x)
{
返回x>=this.Start&&x i.InRange(x));
Console.WriteLine(正确的1.TheValue);
}

这里您还可以使用静态System.Linq.Enumerable的Range()方法来实现

IEnumerable<T>

至少在我看来,这看起来更优雅。

这里您还可以使用静态System.Linq.Enumerable的Range()方法来实现

IEnumerable<T>

至少在我看来,这看起来更优雅。

下一个间隔是否总是从上一个间隔的结尾开始?@Sayse:在我的情况下,是的。下一个间隔是否总是从上一个间隔的结尾开始?@Sayse:在我的情况下,是的。似乎我确实忽略了这一点。:D代码更少,但仍然有很多if。除此之外,没有真正的方法来解决这个问题避免使用大量的if语句,我想你总是可以将math.round和switch语句结合使用,但这并不是真正达到目的。我确实忽略了这一点:D代码更少,但仍然有大量的if语句。除此之外,没有真正的方法避免使用大量的if语句,你总是可以将math.round和switch语句结合使用一个switch语句,我猜,但这并不是真正实现问题中指定的输入。如果您自己的代码在底线上导致NRE,那么这是一个非常简单的模式示例,而不是一个完整的代码