简体   繁体   English

Java中的逻辑表达式

[英]Logical expressions in java

So on Java , an "AND" statement is &&, "OR" statement is ||... 因此,在Java上,“ AND”语句为&&,“ OR”语句为|| ...

What about for XOR then... that is if I have two choices. 那对于XOR呢...那是如果我有两个选择的话。 I HAVE to pick one, but I can't pick both. 我必须选一个,但不能选两个。

HOWEVER, 然而,

private class CheckBoxListener implements ItemListener{
    public void itemStateChanged(ItemEvent e)
    {
    if(one.isSelected()^two.isSelected()){

    thehandler handler = new thehandler();
    button.addActionListener(handler);

        }   

 }}

Even if I have both checkboxes selected, the button is 'enabled'. 即使我同时选中了两个复选框,该按钮也已“启用”。 This is the handler for the button fyi: 这是按钮fyi的处理程序:

private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent event){

    dispose();
}

So if both are selected, and If i click the button. 因此,如果两个都被选中,并且如果我单击按钮。 the frame should not dispose. 框架不应丢弃。 It should only dispose when either one of them is selected. 仅当选择其中之一时,才进行处理。

^ is the XOR operator in Java. ^是Java中的XOR运算符。


Regarding your Swing problem, the problem is that you are not inspecting the state of the checkboxes when the button is clicked, but rather when the checkboxes are selected. 关于Swing问题,问题在于单击按钮时不是在检查复选框的状态,而是在选中复选框时才检查。 You should instead have something like this: 您应该改成这样:

private class ButtonActionListener implements ActionListener {

    /*
     * You will probably define a constructor that accepts the two checkboxes
     * as arguments.
     */

    @Override
    public void actionPerformed(ActionEvent event) {
        if (one.isSelected() ^ two.isSelected()) {
            dispose();
        }
    }
}

The alternative approach is to create one instance of the ActionListener . 另一种方法是创建ActionListener一个实例。 You would add it with addActionListener when exactly one of the checkboxes is checked, and remove it with removeActionListener otherwise: 恰好选中其中一个复选框时,可以使用addActionListener将其添加,否则,可以使用removeActionListener将其删除:

private class CheckBoxListener implements ItemListener {

    private ActionListener buttonActionListener = new thehandler();

    @Override
    public void itemStateChanged(ItemEvent event) {
        if(one.isSelected() ^ two.isSelected()) {
            button.addActionListener(buttonActionListener);
        } else {
            button.removeActionListener(buttonActionListener);
        }
    }
}

You can use the != operator. 您可以使用!=运算符。 For two booleans it has the same truth table as an "actual" XOR operator. 对于两个布尔值,它具有与“实际” XOR运算符相同的真值表。

As pointed out in other answers, the xor operator for boolean (and bitwise) expressions is ^ . 如其他答案所指出的,布尔(和按位)表达式的xor运算符是^

boolean a = true;
boolean b = false;

boolean c = a ^ b;  // c == true

(It should also be noted that & and | works just fine for boolean expressions too.) (还应注意, &|对于布尔表达式也适用。)


So, why does && and || 那么,为什么&&|| exist, but not ^^ ? 存在,但不^^

The explanation is evident if you consider the difference between & and && . 如果考虑&&&的区别,则说明是显而易见的。

The first one ( & ) does not short-circuit the evaluation, while && does. 第一个( & )不会缩短评估,而&&缩短评估。 So logically, ^^ would correspond to the short circuited version of ^ . 因此,从逻辑上讲, ^^将对应于^短路版本。 But, there is no way ^ can be short-circuited (no matter what the first operand evaluates to, the second operand needs to be evaluated) so ^^ would be completely redundant. 但是,不可能将^短路(无论第一个操作数求值,第二个操作数都需要求值),因此^^将是完全多余的。

^是布尔逻辑异或

走的路

boolean xor = a ^ b;

You can do it the long way. 您可以做的很长。 This is a logical XOR. 这是逻辑XOR。

!(p && q) && (p || q)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM