使用Java检查变量是否在两个数字之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21761875/
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
Check if a variable is between two numbers with Java
提问by matthijsW
I have a problem with this code:
我对这段代码有问题:
if (90 >>= angle =<< 180)
The error explanation is:
错误解释是:
The left-hand side of an assignment must be a variable.
赋值的左侧必须是变量。
I understand what this means but how do I turn the above code into correct code?
我明白这意味着什么,但如何将上述代码转换为正确的代码?
采纳答案by AlexWien
I see some errors in your code.
Your probably meant the mathematical term
我在您的代码中看到了一些错误。
你可能指的是数学术语
90 <= angle <= 180, meaning angle in range 90-180.
90 <= 角度 <= 180,表示角度在 90-180 之间。
if (angle >= 90 && angle <= 180) {
// do action
}
回答by aliteralmind
Assuming you are programming in Java, this works:
假设您正在使用 Java 编程,这有效:
if (90 >= angle && angle <= 180 ) {
(don't you mean 90 is lessthan angle
? If so: 90 <= angle
)
(不要你的意思是90较少比angle
如果是这样的:? 90 <= angle
)
回答by peter
are you writing java code for android? in that case you should write maybe
你在为android编写java代码吗?在这种情况下,你应该写
if (90 >= angle && angle <= 180) {
updating the code to a nicer style (like some suggested) you would get:
将代码更新为更好的样式(如某些建议),您将获得:
if (angle <= 90 && angle <= 180) {
now you see that the second check is unnecessary or maybe you mixed up <
and >
signs in the first check and wanted actually to have
现在您看到第二张支票是不必要的,或者您可能混淆<
并>
在第一张支票上签名并实际上想要拥有
if (angle >= 90 && angle <= 180) {
回答by David Ehrmann
<<=
is like +=
, but for a left shift. x <<= 1
means x = x << 1
. That's why 90 >>= angle
doesn't parse. And, like others have said, Java doesn't have an elegant syntax for checking if a number is an an interval, so you have to do it the long way. It also can't do if (x == 0 || 1)
, and you're stuck writing it out the long way.
<<=
就像+=
,但是对于左移。x <<= 1
是指x = x << 1
。这就是为什么90 >>= angle
不解析。而且,就像其他人所说的那样,Java 没有用于检查数字是否为区间的优雅语法,因此您必须走很长的路。它也做不到if (x == 0 || 1)
,而且你一直在写它。
回答by Mchrom
//If "x" is between "a" and "b";
.....
int m = (a+b)/2;
if(Math.abs(x-m) <= (Math.abs(a-m)))
{
(operations)
}
......
......
//have to use floating point conversions if the summ is not even;
//如果总和不偶数,则必须使用浮点转换;
Simple example :
简单的例子:
//if x is between 10 and 20
if(Math.abs(x-15)<=5)
回答by Jay
You can use apache Range API. https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/Range.html
您可以使用 apache Range API。 https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/Range.html
回答by user2569050
public static boolean between(int i, int minValueInclusive, int maxValueInclusive) {
if (i >= minValueInclusive && i <= maxValueInclusive)
return true;
else
return false;
}
https://alvinalexander.com/java/java-method-integer-is-between-a-range
https://alvinalexander.com/java/java-method-integer-is-between-a-range