布爾類的 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 logicalAnd()用法及代碼示例
- Java Boolean logicalOr()用法及代碼示例
- Java Boolean hashCode()用法及代碼示例
- Java Boolean booleanValue()用法及代碼示例
- Java Boolean toString()用法及代碼示例
- Java Boolean compare()用法及代碼示例
- Java Boolean getBoolean()用法及代碼示例
- Java Boolean valueOf()用法及代碼示例
- Java Boolean parseBoolean()用法及代碼示例
- Java Boolean compareTo()用法及代碼示例
- Java Boolean hashcode()用法及代碼示例
- Java Boolean equals()用法及代碼示例
- Java Guava Booleans.concat()用法及代碼示例
- Java Guava Booleans.countTrue()用法及代碼示例
- Java Booleans.indexOf(boolean[] array, boolean[] target)用法及代碼示例
- Java Guava Booleans.lastIndexOf()用法及代碼示例
- Java Guava Booleans.hashCode()用法及代碼示例
- Java Guava Booleans.asList()用法及代碼示例
- Java Guava Booleans.toArray()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Boolean logicalXor() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。