按位运算 Java AND 多个布尔值

发布于 2024-11-16 15:32:27 字数 454 浏览 2 评论 0原文

这是我的问题。我有三个布尔值 是我可以拥有的选择。我可以有多个 与三个选项的组合:

即没有选项(全部为 false) 仅选项 1 选项 1 和选项 2 选项 1 和选项 2 和选项 3 仅选项 2 选项 2 和选项 3 仅选项 3 等等

我需要检查所有组合,但我不 想要编写大量 if else if 语句。

有没有办法获得应有的结果 是?

类似的东西

result = option1 & option2 & option3

,然后我可以进入 switch 语句 处理正确的组合

如果您需要更详细的解释,请告诉我。 提前致谢。

ps我在这里试图做的是避免 这么多 if else if 语句,让我的代码看起来 更干净、设计更好。所以如果你能想到 另一种方式,我会很感激。

谢谢

this is my problem. I have three booleans which
are options I can have. I can have multiple
combinations with the three options:

i.e. no options (all false)
option 1 only
option 1 and option 2
option 1 and option 2 and option 3
option 2 only
option 2 and option 3
option 3 only
etc

I need to check all the combinations, but I don't
want to write a tons of if else if statements.

Is there a way a can obtain what the results should
be?

something like

result = option1 & option2 & option3

and then I can just go in a switch statement to
process the correct combination

Let me know if you need a more detailed explanation.
Thanks in advance.

p.s. what I am trying to do here is to avoid having
so many if else if statements and make my code look
cleaner and better designed. So if you can think of
another way to do, I'd appreciate it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我的奇迹 2024-11-23 15:32:27

您可以为每个可能的结果生成一个Karnaugh_map,使用它的规则您可以简单地计算特定的逻辑将条件集降至最低限度。

不过,我认为最好是为了清楚起见,尝试遵循逻辑流程来了解为什么采用某些分支。如果事情太复杂,也许需要重新考虑一下情况。

You could generate a Karnaugh_map for each possible outcome, using its rules you can simply the logic for a particular set of conditions down to the minimum potentially.

However I think it is best to go for clarity, try following the logic flow for why certain branches are taken. If it is too convoluted maybe rethinking the situation is in order.

孤单情人 2024-11-23 15:32:27

获取交换机中所有 8 个案例的一种方法如下。

将布尔值转换为不同的 int 标志(仅设置一位的值),使用按位或将它们组合起来,然后打开 8 个可能的值。

int combination = (option1 ? 1 : 0) | (option2 ? 2 : 0) | (option3 ? : 4 : 0);

switch(combination) {
case 0: // !1 && !2 && !3
  ...
break;
case 1: // 1 && !2 && !3
  ...
break;
case 2: // !1 && 2 && !3
  ...
break;
case 3: // 1 && 2 && !3
  ...
break;
case 4: // !1 && !2 && 3
  ...
break;
case 5: // 1 && !2 && 3
  ...
break;
case 6: // !1 && 2 && 3
  ...
break;
case 7: // 1 && 2 && 3
  ...
break;
}

通过这种方法,您可以平等地处理所有 8 种情况。但如果添加更多的布尔值,它就会变得失控,因为组合的数量呈指数级增长。

One way to get hold of all 8 cases in a switch is the following.

Convert the booleans to different int flags (values with only one bit set), combine these with bitwise OR, and then switch on the 8 possible values.

int combination = (option1 ? 1 : 0) | (option2 ? 2 : 0) | (option3 ? : 4 : 0);

switch(combination) {
case 0: // !1 && !2 && !3
  ...
break;
case 1: // 1 && !2 && !3
  ...
break;
case 2: // !1 && 2 && !3
  ...
break;
case 3: // 1 && 2 && !3
  ...
break;
case 4: // !1 && !2 && 3
  ...
break;
case 5: // 1 && !2 && 3
  ...
break;
case 6: // !1 && 2 && 3
  ...
break;
case 7: // 1 && 2 && 3
  ...
break;
}

With this approach, you can handle all 8 cases equally. But it will grow out of hands if there are more booleans added, because the number of combinations grows exponentially.

栀梦 2024-11-23 15:32:27

我的解决方案不适用于布尔值,但您可以调整它。例如,调用 Option.CombinedOption.get(Option.ONE, Option.THREE) 将返回枚举 CombinedOption.OPTION_1_3。

public enum Option {
ONE, TWO, THREE;

public enum CombinedOption {

    ALL_FASLSE(), OPTION_1(ONE), OPTION_1_2(ONE, TWO), OPTION_1_3(ONE,
            THREE), OPTION_1_2_3(ONE, TWO, THREE), OPTION_2(TWO), OPTION_2_3(
            TWO, THREE), OPTION_3(THREE);

    private Set<Option> keySet;

    private CombinedOption(Option... options) {
        if (options != null && options.length > 0)
            keySet = EnumSet.copyOf(Arrays.asList(options));
        else
            keySet = EnumSet.noneOf(Option.class);

    }

    private static final Map<Set<Option>, CombinedOption> optionMapping = new HashMap<Set<Option>, CombinedOption>();

    static {
        for (CombinedOption combined : CombinedOption.values()) {
            optionMapping.put(combined.keySet, combined);
        }
    }

    public static CombinedOption get(Option... options) {
        Set<Option> keySet;

        if (options != null && options.length > 0)
            keySet = EnumSet.copyOf(Arrays.asList(options));
        else
            keySet = EnumSet.noneOf(Option.class);

        return optionMapping.get(keySet);
    }
}

}

My solution does not work with booleans, but you could adapt it. For example the call of Option.CombinedOption.get(Option.ONE, Option.THREE) returns the enum CombinedOption.OPTION_1_3.

public enum Option {
ONE, TWO, THREE;

public enum CombinedOption {

    ALL_FASLSE(), OPTION_1(ONE), OPTION_1_2(ONE, TWO), OPTION_1_3(ONE,
            THREE), OPTION_1_2_3(ONE, TWO, THREE), OPTION_2(TWO), OPTION_2_3(
            TWO, THREE), OPTION_3(THREE);

    private Set<Option> keySet;

    private CombinedOption(Option... options) {
        if (options != null && options.length > 0)
            keySet = EnumSet.copyOf(Arrays.asList(options));
        else
            keySet = EnumSet.noneOf(Option.class);

    }

    private static final Map<Set<Option>, CombinedOption> optionMapping = new HashMap<Set<Option>, CombinedOption>();

    static {
        for (CombinedOption combined : CombinedOption.values()) {
            optionMapping.put(combined.keySet, combined);
        }
    }

    public static CombinedOption get(Option... options) {
        Set<Option> keySet;

        if (options != null && options.length > 0)
            keySet = EnumSet.copyOf(Arrays.asList(options));
        else
            keySet = EnumSet.noneOf(Option.class);

        return optionMapping.get(keySet);
    }
}

}

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