Java 比较三个整数是否相等的替代方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20533309/
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:34:58  来源:igfitidea点击:

alternative way to compare if three ints are equal

javaoperatorsconditional-statements

提问by user3059427

int a = 10;
int b = 10;
int c = 10;

I am trying to find out if there is alternate way to compare if three ints or anything are equal.

我试图找出是否有替代方法来比较三个整数或任何东西是否相等。

The way i am currently doing is

我目前正在做的方式是

if( (a == b) && (a == c) && (b == c)){

}

I was wondering if there is an alternate and more concise way to do this.

我想知道是否有另一种更简洁的方法来做到这一点。

采纳答案by rgettman

Equality is transitive; you don't need the last comparison b == c. If a == band a == c, then b == c.

平等是可传递的;你不需要最后的比较b == c。如果a == ba == c,那么b == c

Try

尝试

if ((a == b) && (a == c)){

回答by PM 77-1

If all you need is to know whether three are equal then you can use:

如果您只需要知道三个是否相等,那么您可以使用:

if ((a==b) && (b==c)) {

}

回答by I?ya Bursov

alternative variant:

替代变体:

int t1 = a-b;
int t2 = c-b;
if ( (t1|t2) == 0 ) // both are equal