比较Java中的4个值

huangapple go评论63阅读模式
英文:

Compare 4 values in Java

问题

如果有一种方法可以简单地执行以下操作,而不会因比较字节与布尔值而出现错误:

f1.getPlayer() == f2.getPlayer() == f3.getPlayer() != (byte) 0

所有这些函数都返回字节。

我唯一能想到的选项是将f1.getPlayer()与其他所有对象进行比较,但如果确实没有其他办法,那将是我的备用解决方案。

我知道使用数组会更容易,但我想尝试一下不使用它。

英文:

I was wondering, if there was a way one can simply do

f1.getPlayer()==f2.getPlayer()==f3.getPlayer()!=(byte)0

without getting an error for comparing byte with boolean.
All the functions return bytes.

The only option I could come up with was to compare f1.getPlayer() to all other objects, but that would be my backup solution if there is truly no other way.

I know, that it would be way easier with arrays, but I wanted to try it without.

答案1

得分: 2

如果您正在使用Java 8或更高版本,您可以使用Streams。以下是一个示例解决方案:

boolean allMatchAndNonZero = Stream
    .of(f1, f2, f3)
    .allMatch(f -> f.getPlayer() != (byte) 0 && f.getPlayer() == f1.getPlayer());
英文:

If you are using the Java version 8 or above, you can use Streams. Following is an example solution :

boolean allMatchAndNonZero = Stream
    .of(f1,f2,f3)
    .allMatch(f -> f.getPlayer() != (byte)0 && f.getPlayer() == f1.getPlayer());

huangapple
  • 本文由 发表于 2023年2月24日 06:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定