当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Boolean logicalXor()用法及代码示例


布尔类的 logicalXor() 方法返回对分配的布尔操作数执行逻辑异或运算的结果。

用法:

public static boolean logicalXor(boolean a, boolean b)

参数:

传递的参数是 a(第一个布尔操作数)和 b(第二个布尔操作数)。

返回值:

logicalXor() 方法返回对布尔参数 a 和 b 应用逻辑异或运算的结果。

  • 如果传递的两个布尔操作数不同,则返回 true。
  • 如果传递的两个布尔操作数相同,则返回 false。

例子1

public class BooleanLogicalXorExample1 {
    public static void main(String[] args) {

        Boolean b1 = true;
        Boolean b2 = false;
        //if the bool values are different logicalXor() method will return true.
        Boolean b3 = Boolean.logicalXor(b1, b2);
        System.out.println("1. logicalXor() method for "+b1+" "+b2+" is "+b3);

        Boolean b4 = true;
        Boolean b5 = true;
        //if the bool value are same logicalXor() method will return false.
        Boolean b6 = Boolean.logicalXor(b4, b5);
        System.out.println("2. logicalXor() method for "+b4+" "+b5+" is "+b6);

        Boolean b7 = false;
        Boolean b8 = false;
        System.out.println("3. logicalXor() method for "+b7+" "+b8+" is "+Boolean.logicalXor(b7,b8));

    }
    }

输出:

1. logicalXor() method for true false is true
2. logicalXor() method for true true is false
3. logicalXor() method for false false is false

例子2

import java.util.Scanner;

public class BooleanLogicalXorExample2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Boolean b1 = false;
        Boolean b2 = false;
        System.out.println("Enter your age");
        int a = scanner.nextInt();
        if(a>17) {
            b2 = true;
        }
        boolean b3 = Boolean.logicalXor(b1,b2);

        // calling static hash code method
         if (b3==true) {
             System.out.println("logical Xor is "+b3);
            System.out.println("You are an adult.\nYou are eligible to vote.");
        }
        else{
             System.out.println("Logical Xor is "+b3);
            int val2 =18-a;
            System.out.println("You are not an adult.\nYou will be eligible to vote after "+val2+" years.");
        }
    }
}

输出:

Enter your age
45
logical Xor is true 
You are an adult.
You are eligible to vote.

例子3

import java.util.Scanner;

public class BooleanLogicalXorExample3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); 
        int a, b;
        Boolean b1 = true;
        Boolean b2 = true;
        System.out.println("Enter two no.s");
        System.out.print("I no:");
        a = scanner.nextInt();
        System.out.print("II no:");
        b = scanner.nextInt();
        if(b>a){
            b1=false;
        }
        if (a == b) {
            System.out.println("Try with different numbers.");

        } else if (a!=b) {
            boolean b3 = Boolean.logicalXor(b1, b2);

            if (b3 == true) {
                System.out.println(b + " is greater");

            } else {
                System.out.println(a + " is greater");
            }
        }
    }
}

输出:

Enter two no.s
I no:45
II no:89
89 is greater





相关用法


注:本文由纯净天空筛选整理自 Java Boolean logicalXor() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。