是否有更紧凑的方法来检查数字是否在某个范围内?

发布于 2024-10-07 07:51:31 字数 249 浏览 1 评论 0 原文

我希望能够测试一个值是否在数字范围内。这是我当前的代码:

if ((year < 2099) && (year > 1990)){
    return 'good stuff';
}

有更简单的方法吗?例如,有这样的事情吗?

if (1990 < year < 2099){
    return 'good stuff';
}

I want to be able to test whether a value is within a number range. This is my current code:

if ((year < 2099) && (year > 1990)){
    return 'good stuff';
}

Is there a simpler way to do this? For example, is there something like this?

if (1990 < year < 2099){
    return 'good stuff';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春庭雪 2024-10-14 07:51:32

来自此处的类似解决方案: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is- Between-two-numbers/

$.fn.between = function(a,b){
    return (a < b ? this[0] >= a && this[0] <= b : this[0] >= b && this[0] <= a);
}

From a similar solution here: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/

$.fn.between = function(a,b){
    return (a < b ? this[0] >= a && this[0] <= b : this[0] >= b && this[0] <= a);
}
半窗疏影 2024-10-14 07:51:31

在许多语言中,第二种方式将根据您想要的内容从左到右错误地进行评估。

例如,在 C 语言中,1990 < year 将计算为 0 或 1,然后变为 1 < 2099,当然,这总是正确的。

Javascript 与 C 非常相似:1990 < year 返回 truefalse,这些布尔表达式在数字上看起来分别等于 0 和 1。

但在 C# 中,它甚至无法编译,并给出错误:

错误 CS0019:运算符“<”不能应用于“bool”和“int”类型的操作数

您会从 Ruby 中得到类似的错误,而 Haskell 会告诉您不能在同一个中缀表达式中使用 < 两次。

在我的脑海中,Python 是唯一一种我确信能够以这种方式处理“之间”设置的语言:

>>> year = 5
>>> 1990 < year < 2099
False
>>> year = 2000
>>> 1990 < year < 2099
True

底线是第一种方式 (x 始终是您最安全的选择。

In many languages, the second way will be evaluated from left to right incorrectly with regard to what you want.

In C, for instance, 1990 < year will evaluate to 0 or 1, which then becomes 1 < 2099, which is always true, of course.

Javascript is a quite similar to C: 1990 < year returns true or false, and those boolean expressions seem to numerically compare equal to 0 and 1 respectively.

But in C#, it won't even compile, giving you the error:

error CS0019: Operator '<' cannot be applied to operands of type 'bool' and 'int'

You get a similar error from Ruby, while Haskell tells you that you cannot use < twice in the same infix expression.

Off the top of my head, Python is the only language that I'm sure handles the "between" setup that way:

>>> year = 5
>>> 1990 < year < 2099
False
>>> year = 2000
>>> 1990 < year < 2099
True

The bottom line is that the first way (x < y && y < z) is always your safest bet.

画骨成沙 2024-10-14 07:51:31

您可以制定自己的方法:

// jquery
$(function() {
    var myNumber = 100;
    try {
        if (myNumber.isBetween(50, 150)) 
            alert(myNumber + " is between 50 and 100.");
        else 
            alert(myNumber + " is not between 50 and 100.");
    } catch (e) {
        alert(e.message());
    }

});

// js prototype
if (typeof(Number.prototype.isBetween) === "undefined") {
    Number.prototype.isBetween = function(min, max, notBoundaries) {
        var between = false;
        if (notBoundaries) {
            if ((this < max) && (this > min)) between = true;
            alert('notBoundaries');
        } else {
            if ((this <= max) && (this >= min)) between = true;
            alert('Boundaries');
        }
        alert('here');
        return between;
    }
}

希望这会有所帮助。

最大限度

You could make your own method:

// jquery
$(function() {
    var myNumber = 100;
    try {
        if (myNumber.isBetween(50, 150)) 
            alert(myNumber + " is between 50 and 100.");
        else 
            alert(myNumber + " is not between 50 and 100.");
    } catch (e) {
        alert(e.message());
    }

});

// js prototype
if (typeof(Number.prototype.isBetween) === "undefined") {
    Number.prototype.isBetween = function(min, max, notBoundaries) {
        var between = false;
        if (notBoundaries) {
            if ((this < max) && (this > min)) between = true;
            alert('notBoundaries');
        } else {
            if ((this <= max) && (this >= min)) between = true;
            alert('Boundaries');
        }
        alert('here');
        return between;
    }
}

hope this helps.

Max

拧巴小姐 2024-10-14 07:51:31

实现此目的的快速而简单的方法是创建一个如下所示的函数:

function inRange(n, nStart, nEnd)
{
    if(n>=nStart && n<=nEnd) return true;
    else return false;
}

然后按如下方式使用它:

inRange(500, 200, 1000) => this return true;

或者像这样:

inRange(199, 200, 1000) => this return false;

The fast and simple way to make this is to create a function like this:

function inRange(n, nStart, nEnd)
{
    if(n>=nStart && n<=nEnd) return true;
    else return false;
}

Then use that as follows:

inRange(500, 200, 1000) => this return true;

Or like this:

inRange(199, 200, 1000) => this return false;
向地狱狂奔 2024-10-14 07:51:31

如果您不喜欢布尔运算符,您可以随时使用嵌套的 if 语句:

if (1990 < year)
{
    if( year < 2099)
        return 'good stuff';
}

If you don't like the boolean operator, you could always use nested if statements:

if (1990 < year)
{
    if( year < 2099)
        return 'good stuff';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文