或者 if 语句中带有 int 的操作数

发布于 2024-08-16 09:12:12 字数 286 浏览 7 评论 0原文

我的问题是程序没有按照我的意图“他”读取代码。

我有

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

可是我不能没有你 2024-08-23 09:12:12

使用:

if (hero.getPos() == 6 || hero.getPos() == 11 || hero.getPos() == 16)) {

这将实现您想要的功能。

您所做的是将 hero.getPos()(6|11|16) 的结果进行比较,这将执行 这些数字之间的按位或

Use:

if (hero.getPos() == 6 || hero.getPos() == 11 || hero.getPos() == 16)) {

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.

萌面超妹 2024-08-23 09:12:12

其他答案是正确的,只是以不同的方式思考你可以使用集合。

static final Set<Integer> positions = new HashSet<Integer>();
static{
    positions.add(6);
    positions.add(11);
    positions.add(16);
}

if (positions.contains(hero.getPos())){
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

The other answers are correct, just thinking differently you may use Sets.

static final Set<Integer> positions = new HashSet<Integer>();
static{
    positions.add(6);
    positions.add(11);
    positions.add(16);
}

if (positions.contains(hero.getPos())){
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}
苍白女子 2024-08-23 09:12:12

你不能那样做。它按位或 3 的数字。

你必须这样做:

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

你看到区别了吗? | 是按位或,而 || 是逻辑或。
另请注意,您每次都必须重写比较。

You cannot do it like that. It ors the 3 number bitwise.

You have to do like this :

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

You see the difference ? | is a bitwise or while || is a logical or.
Note also that you have to rewrite the comparison each time.

原谅过去的我 2024-08-23 09:12:12

(6 | 11 | 16) 将首先计算为 31(二元运算),即 6 != 31。不是你想要的。

更好的是检查每个位置(你只有 3 个位置,所以内联很好,更多考虑使用循环):

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

(6 | 11 | 16) would be evaluated first to 31 (binary operation), which is 6 != 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):

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}
孤者何惧 2024-08-23 09:12:12

不,您需要检查每个值的 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 check myCollection.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.

不奢求什么 2024-08-23 09:12:12

使用以下答案:

如何我可以测试一个数组是否包含某个值吗?

您可以创建一个数字数组并检查您的 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.

甜妞爱困 2024-08-23 09:12:12

不。您可以创建一个 Set 一次,然后使用它,或者只是:

int number = ci.getNumber();
if (number == 6252001 || number == 5855797 || number == 6251999)

我还会考虑将这些数字更改为常量,以便获得更有意义的代码。

No. You could create a Set<Integer> once and then use that, or just:

int number = ci.getNumber();
if (number == 6252001 || number == 5855797 || number == 6251999)

I'd also consider changing those numbers into constants so that you get more meaningful code.

靖瑶 2024-08-23 09:12:12

没有这样的运算符。但如果你要比较数字,你可以使用 switch 来模拟。方法如下:

int aNumber = ci.getNumber();
swithc(aNumber) {
    case 6252001:
    case 5855797:
    case 6251999: {
        ...
        break;
    }
    default: {
        ... // Do else.
    }
}

希望这会有所帮助。

There is no such operator. But if you are comparing number, you can use switch do simulate that. Here is how:

int aNumber = ci.getNumber();
swithc(aNumber) {
    case 6252001:
    case 5855797:
    case 6251999: {
        ...
        break;
    }
    default: {
        ... // Do else.
    }
}

Hope this helps.

枕花眠 2024-08-23 09:12:12
boolean theyAretheSame = num1 == num2 ? (num1 == num3 ? true:false):false;

我必须承认我没有检查过这一点,但逻辑看起来是正确的。

boolean theyAretheSame = num1 == num2 ? (num1 == num3 ? true:false):false;

I must admit I haven't checked this but the logic looks correct.

一直在等你来 2024-08-23 09:12:12

您可以将所有数字放入一个集合中,然后使用 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.

去了角落 2024-08-23 09:12:12

Java 不会让你这样做。你可以做一个哈希查找(这对于这个来说是多余的)或一个 case 语句,或者一个大的丑陋的多重比较:

if ((n==1 ) || (n==2) || ...

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:

if ((n==1 ) || (n==2) || ...
葬﹪忆之殇 2024-08-23 09:12:12

不..你必须单独比较它们。

no.. you have to compare them individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文