英文:
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论