或者 if 语句中带有 int 的操作数
我的问题是程序没有按照我的意图“他”读取代码。
我有
if (hero.getPos() == (6 | 11 | 16)) {
move = new Object[] {"Up", "Right", "Left"};
} else {
move = new Object[] {"Up", "Down", "Right", "Left"};
}
当英雄位置为6时,程序仍然转到其他位置。
这是为什么?是因为操作数吗?如果是,我应该如何更改?
My problem is that program is not reading codes as i intended "he" would.
I have
if (hero.getPos() == (6 | 11 | 16)) {
move = new Object[] {"Up", "Right", "Left"};
} else {
move = new Object[] {"Up", "Down", "Right", "Left"};
}
When hero position is 6, the program still goes to else.
Why is that? Is it because of operands? If yes, how should i change it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用:
这将实现您想要的功能。
您所做的是将
hero.getPos()
与(6|11|16)
的结果进行比较,这将执行 这些数字之间的按位或。Use:
This will do what you want.
What you did is comparing
hero.getPos()
with the result of(6|11|16)
which will do bitwise or between those numbers.其他答案是正确的,只是以不同的方式思考你可以使用集合。
The other answers are correct, just thinking differently you may use Sets.
你不能那样做。它按位或 3 的数字。
你必须这样做:
你看到区别了吗?
|
是按位或,而||
是逻辑或。另请注意,您每次都必须重写比较。
You cannot do it like that. It ors the 3 number bitwise.
You have to do like this :
You see the difference ?
|
is a bitwise or while||
is a logical or.Note also that you have to rewrite the comparison each time.
(6 | 11 | 16)
将首先计算为 31(二元运算),即6 != 31
。不是你想要的。更好的是检查每个位置(你只有 3 个位置,所以内联很好,更多考虑使用循环):
(6 | 11 | 16)
would be evaluated first to 31 (binary operation), which is6 != 31
. Not what you want.Better is to check every single position (you have only 3, so inline is good, for more consider using a loop):
不,您需要检查每个值的
ci.getNumber() == ...
,或者将它们添加到集合中并检查myCollection.contains(ci.getNumber( ))
。但是,如果您要根据多个已知值检查方法,您可能需要重新考虑代码的结构。No, you're going to need to check
ci.getNumber() == ...
for each value, or add them to a collection and checkmyCollection.contains(ci.getNumber())
. However, you may want to re-think the structure of your code if you are checking a method against several known values.使用以下答案:
如何我可以测试一个数组是否包含某个值吗?
您可以创建一个数字数组并检查您的 ci.getNumber() 是否在其中。
using the answer from:
How can I test if an array contains a certain value?
you could create an array of numbers and check if your ci.getNumber() is in it.
不。您可以创建一个
Set
一次,然后使用它,或者只是:我还会考虑将这些数字更改为常量,以便获得更有意义的代码。
No. You could create a
Set<Integer>
once and then use that, or just:I'd also consider changing those numbers into constants so that you get more meaningful code.
没有这样的运算符。但如果你要比较数字,你可以使用 switch 来模拟。方法如下:
希望这会有所帮助。
There is no such operator. But if you are comparing number, you can use switch do simulate that. Here is how:
Hope this helps.
我必须承认我没有检查过这一点,但逻辑看起来是正确的。
I must admit I haven't checked this but the logic looks correct.
您可以将所有数字放入一个集合中,然后使用
contains()
方法。除此之外,我不认为有任何特殊的语法可以像您想要的那样进行比较。You could put all the numbers in a collection, and then use the
contains()
method. Other than that, I don't believe there is any special syntax for comparing like you want to do.Java 不会让你这样做。你可以做一个哈希查找(这对于这个来说是多余的)或一个 case 语句,或者一个大的丑陋的多重比较:
Java won't let you do that. You can do a hash lookup (which is overkill for this) or a case statement, or a big honking ugly multiple compare:
不..你必须单独比较它们。
no.. you have to compare them individually.