Java 检查 int 是否在两个数字之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1992970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:13:18  来源:igfitidea点击:

Check if int is between two numbers

javaintegerconditional-operator

提问by Timo Willemsen

Why can't do you this if you try to find out whether an int is between to numbers:

如果您试图找出 int 是否介于数字之间,为什么不能这样做:

if(10 < x < 20)

Instead of it, you'll have to do

而不是它,你必须做

if(10<x && x<20)

which seems like a bit of overhead.

这似乎有点开销。

采纳答案by Stephen C

One problem is that a ternary relational construct would introduce serious parser problems:

一个问题是三元关系结构会引入严重的解析器问题:

<expr> ::= <expr> <rel-op> <expr> |
           ... |
           <expr> <rel-op> <expr> <rel-op> <expr>

When you try to express a grammar with those productions using a typical PGS, you'll find that there is a shift-reduce conflict at the point of the first <rel-op>. The parse needs to lookahead an arbitrary number of symbols to see if there is a second <rel-op>before it can decide whether the binary or ternary form has been used. In this case, you could not simply ignore the conflict because that would result in incorrect parses.

当您尝试使用典型的 PGS 用这些产生式表达语法时,您会发现在第一个<rel-op>. 解析需要先看任意数量的符号,看看是否还有一秒钟的<rel-op>时间才能决定是使用二进制还是三进制形式。在这种情况下,您不能简单地忽略冲突,因为这会导致错误的解析。

I'm not saying that this grammar is fatally ambiguous. But I think you'd need a backtracking parser to deal with it correctly. And that is a serious problem for a programming language where fast compilation is a major selling point.

我并不是说这个语法是致命的模棱两可。但我认为你需要一个回溯解析器来正确处理它。对于以快速编译为主要卖点的编程语言来说,这是一个严重的问题。

回答by Marc Gravell

Because that syntax simply isn't defined? Besides, x < yevaluates as a bool, so what does bool < intmean? It isn't really an overhead; besides, you could write a utility method if you reallywant - isBetween(10,x,20)- I wouldn't myself, but hey...

因为那个语法根本没有定义?此外,x < y评估为布尔值,那bool < int是什么意思?这并不是真正的开销。此外,如果你真的想要,你可以写一个实用方法- isBetween(10,x,20)- 我自己不会,但是嘿......

回答by Michael Petrotta

Because the <operator (and most others) are binary operators (they take two arguments), and (true true)is not a valid boolean expression.

因为<运算符(和大多数其他运算符)是二元运算符(它们带有两个参数),并且(true true)不是有效的布尔表达式。

The Java language designers could have designed the language to allow syntax like the type you prefer, but (I'm guessing) they decided that it was not worth the more complex parsing rules.

Java 语言设计者本可以将语言设计为允许使用您喜欢的类型的语法,但是(我猜)他们认为不值得使用更复杂的解析规则。

回答by Paul Tomblin

It's just the syntax. '<' is a binary operation, and most languages don't make it transitive. They could have made it like the way you say, but then somebody would be asking why you can't do other operations in trinary as well. "if (12 < x != 5)"?

这只是语法。'<' 是一个二元运算,大多数语言都没有使它具有可传递性。他们可以像你说的那样做,但是有人会问为什么你不能在三进制中做其他操作。“如果 (12 < x != 5)”?

Syntax is always a trade-off between complexity, expressiveness and readability. Different language designers make different choices. For instance, SQL has "x BETWEEN y AND z", where x, y, and z can individually or all be columns, constants, or bound variables. And I'm happy to use it in SQL, and I'm equally happy not to worry about why it's not in Java.

语法总是复杂性、表达性和可读性之间的权衡。不同的语言设计者做出不同的选择。例如,SQL 有“x BETWEEN y AND z”,其中 x、y 和 z 可以单独或全部为列、常量或绑定变量。我很高兴在 SQL 中使用它,我同样很高兴不用担心为什么它不在 Java 中。

回答by TofuBeer

COBOL allows that (I am sure some other languages do as well). Java inherited most of it's syntax from C which doesn't allow it.

COBOL 允许(我相信其他一些语言也可以)。Java 从 C 继承了它的大部分语法,这是不允许的。

回答by Oren

You are human, and therefore you understand what the term "10 < x < 20" suppose to mean. The computer doesn't have this intuition, so it reads it as: "(10 < x) < 20".

您是人类,因此您理解术语“10 < x < 20”的含义。计算机没有这种直觉,所以它读作:“(10 < x) < 20”。

For example, if x = 15, it will calculate:

例如,如果 x = 15,它将计算:

(10 < x) => TRUE

(10 < x) => 真

"TRUE < 20" => ???

“真 < 20” => ???

In C programming, it will be worse, since there are no True\False values. If x = 5, the calculation will be:

在 C 编程中,情况会更糟,因为没有 True\False 值。如果 x = 5,计算将是:

10 < x => 0 (the value of False)

10 < x => 0(False 的值)

0 < 20 => non-0 number (True)

0 < 20 => 非 0 数字(真)

and therefore "10 < 5 < 20" will return True! :S

因此“10 < 5 < 20”将返回 True!:S

回答by starblue

The inconvenience of typing 10 < x && x < 20is minimal compared to the increase in language complexity if one would allow 10 < x < 20, so the designers of the Java language decided against supporting it.

10 < x && x < 20如果允许的话10 < x < 20,与语言复杂性的增加相比,打字带来的不便微乎其微,因此 Java 语言的设计者决定不支持它。

回答by IceSteve

You could make your own

你可以自己做

public static boolean isBetween(int a, int b, int c) {
    return b > a ? c > a && c < b : c > b && c < a;
}

Edit: sorry checks if c is between a and b

编辑:抱歉检查 c 是否在 a 和 b 之间

回答by Nakabo

if (10 < x || x < 20)

if (10 < x || x < 20)

This statement will evaluate true for numbers between 10 and 20. This is a rough equivalent to 10 < x < 20

此语句将评估 10 到 20 之间的数字为真。这大致相当于 10 < x < 20

回答by Sergio76

simplifying:

简化:

a = 10; b = 15; c = 20

public static boolean check(int a, int b, int c) {
    return a<=b && b<=c;
}

This checks if b is between a and c

这检查 b 是否在 a 和 c 之间